Skip to content

Instantly share code, notes, and snippets.

@okitan
Created October 10, 2011 06:45
Show Gist options
  • Save okitan/1274764 to your computer and use it in GitHub Desktop.
Save okitan/1274764 to your computer and use it in GitHub Desktop.
api to return error status
$: << '.'
require 'test_app'
use Rack::ContentType
use Rack::ContentLength
run TestApp
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