Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 0f89cf4dba270375071e99ef9cb00d9a7a9523a9 1 parent 19117ff
Jeremy Kemper authored May 08, 2013
4  actionpack/lib/action_dispatch/middleware/public_exceptions.rb
@@ -11,7 +11,7 @@ def call(env)
11 11
       status       = env["PATH_INFO"][1..-1]
12 12
       request      = ActionDispatch::Request.new(env)
13 13
       content_type = request.formats.first
14  
-      body         = { :status => status, :error => exception.message }
  14
+      body         = { :status => status, :error => Rack::Utils::HTTP_STATUS_CODES.fetch(status.to_i, Rack::Utils::HTTP_STATUS_CODES[500]) }
15 15
 
16 16
       render(status, content_type, body)
17 17
     end
@@ -19,7 +19,7 @@ def call(env)
19 19
     private
20 20
 
21 21
     def render(status, content_type, body)
22  
-      format = content_type && "to_#{content_type.to_sym}"
  22
+      format = "to_#{content_type.to_sym}" if content_type
23 23
       if format && body.respond_to?(format)
24 24
         render_format(status, content_type, body.public_send(format))
25 25
       else
4  actionpack/test/controller/show_exceptions_test.rb
@@ -75,7 +75,7 @@ def test_render_json_exception
75 75
       get "/", {}, 'HTTP_ACCEPT' => 'application/json'
76 76
       assert_response :internal_server_error
77 77
       assert_equal 'application/json', response.content_type.to_s
78  
-      assert_equal({ :status => '500', :error => 'boom!' }.to_json, response.body)
  78
+      assert_equal({ :status => '500', :error => 'Internal Server Error' }.to_json, response.body)
79 79
     end
80 80
 
81 81
     def test_render_xml_exception
@@ -83,7 +83,7 @@ def test_render_xml_exception
83 83
       get "/", {}, 'HTTP_ACCEPT' => 'application/xml'
84 84
       assert_response :internal_server_error
85 85
       assert_equal 'application/xml', response.content_type.to_s
86  
-      assert_equal({ :status => '500', :error => 'boom!' }.to_xml, response.body)
  86
+      assert_equal({ :status => '500', :error => 'Internal Server Error' }.to_xml, response.body)
87 87
     end
88 88
 
89 89
     def test_render_fallback_exception

0 notes on commit 0f89cf4

Please sign in to comment.
Something went wrong with that request. Please try again.