Fix that JSON and XML exception responses should give the HTTP error message for their status, by default, not the message from the underlying exception

This commit is contained in:
Jeremy Kemper 2013-05-08 21:28:27 -07:00
parent c0df1e8c08
commit 5e03239d59
2 changed files with 4 additions and 4 deletions

@ -11,7 +11,7 @@ def call(env)
status = env["PATH_INFO"][1..-1] status = env["PATH_INFO"][1..-1]
request = ActionDispatch::Request.new(env) request = ActionDispatch::Request.new(env)
content_type = request.formats.first content_type = request.formats.first
body = { :status => status, :error => exception.message } body = { :status => status, :error => Rack::Utils::HTTP_STATUS_CODES.fetch(status.to_i, Rack::Utils::HTTP_STATUS_CODES[500]) }
render(status, content_type, body) render(status, content_type, body)
end end
@ -19,7 +19,7 @@ def call(env)
private private
def render(status, content_type, body) def render(status, content_type, body)
format = content_type && "to_#{content_type.to_sym}" format = "to_#{content_type.to_sym}" if content_type
if format && body.respond_to?(format) if format && body.respond_to?(format)
render_format(status, content_type, body.public_send(format)) render_format(status, content_type, body.public_send(format))
else else

@ -75,7 +75,7 @@ def test_render_json_exception
get "/", {}, 'HTTP_ACCEPT' => 'application/json' get "/", {}, 'HTTP_ACCEPT' => 'application/json'
assert_response :internal_server_error assert_response :internal_server_error
assert_equal 'application/json', response.content_type.to_s assert_equal 'application/json', response.content_type.to_s
assert_equal({ :status => '500', :error => 'boom!' }.to_json, response.body) assert_equal({ :status => '500', :error => 'Internal Server Error' }.to_json, response.body)
end end
def test_render_xml_exception def test_render_xml_exception
@ -83,7 +83,7 @@ def test_render_xml_exception
get "/", {}, 'HTTP_ACCEPT' => 'application/xml' get "/", {}, 'HTTP_ACCEPT' => 'application/xml'
assert_response :internal_server_error assert_response :internal_server_error
assert_equal 'application/xml', response.content_type.to_s assert_equal 'application/xml', response.content_type.to_s
assert_equal({ :status => '500', :error => 'boom!' }.to_xml, response.body) assert_equal({ :status => '500', :error => 'Internal Server Error' }.to_xml, response.body)
end end
def test_render_fallback_exception def test_render_fallback_exception