1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

give access to the original exception raised in case of a parse error

This commit is contained in:
Aaron Patterson 2012-10-25 11:34:37 -07:00
parent facd3e8710
commit cde326bcef
3 changed files with 23 additions and 2 deletions

View file

@ -3,6 +3,13 @@ module ActionController
end
class BadRequest < ActionControllerError #:nodoc:
attr_reader :original_exception
def initialize(type, e)
super("Invalid #{type} parameters: #{e.message}")
@original_exception = e
set_backtrace e.backtrace
end
end
class RenderError < ActionControllerError #:nodoc:

View file

@ -253,7 +253,7 @@ module ActionDispatch
def GET
@env["action_dispatch.request.query_parameters"] ||= (normalize_parameters(super) || {})
rescue TypeError => e
raise ActionController::BadRequest, "Invalid query parameters: #{e.message}", e.backtrace
raise ActionController::BadRequest.new(:query, e)
end
alias :query_parameters :GET
@ -261,7 +261,7 @@ module ActionDispatch
def POST
@env["action_dispatch.request.request_parameters"] ||= (normalize_parameters(super) || {})
rescue TypeError => e
raise ActionController::BadRequest, "Invalid request parameters: #{e.message}", e.backtrace
raise ActionController::BadRequest.new(:request, e)
end
alias :request_parameters :POST

View file

@ -560,6 +560,20 @@ class RequestTest < ActiveSupport::TestCase
assert_equal({}, request.parameters)
end
test "we have access to the original exception" do
mock_rack_env = { "QUERY_STRING" => "x[y]=1&x[y][][w]=2", "rack.input" => "foo" }
request = nil
request = stub_request(mock_rack_env)
e = assert_raises(ActionController::BadRequest) do
# rack will raise a TypeError when parsing this query string
request.parameters
end
assert e.original_exception
assert_equal e.original_exception.backtrace, e.backtrace
end
test "formats with accept header" do
request = stub_request 'HTTP_ACCEPT' => 'text/html'
request.expects(:parameters).at_least_once.returns({})