Created
October 10, 2011 06:45
-
-
Save okitan/1274764 to your computer and use it in GitHub Desktop.
api to return error status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$: << '.' | |
require 'test_app' | |
use Rack::ContentType | |
use Rack::ContentLength | |
run TestApp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
require 'json' | |
module ErrorResponse | |
def render_json(status, message_id, opt={}) | |
[ status, opt.merge({ 'Content-Type' => 'application/json' }), [ { 'message_id' => message_id }.to_json ] ] | |
end | |
def self.included(base) | |
base.module_eval do | |
# WWW-Authenticate response-header field MUST be included in 401 | |
# but, I don't know its syntax | |
get('/errors/401') { render_json(401, 'client error', 'WWW-Authenticate' => 'some chalenge') } | |
# Allow | |
get('/errors/405') { render_json(405, 'client error', 'Allow' => 'POST' ) } | |
# Proxy-Authenticate | |
get('/errors/407') { render_json(407, 'client error', 'Proxy-Authenticate' => 'some challeng') } | |
# TODO: 413 Retry-After (when temporary) | |
(400..426).each do |status| | |
get("/errors/#{status}") { render_json(status, 'client error') } | |
end | |
(500..510).each do |status| | |
get("/errors/#{status}") { render_json(status, 'server error') } | |
end | |
end | |
end | |
end | |
class TestApp < Sinatra::Base | |
include ::ErrorResponse | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment