diff --git a/actionpack/lib/action_controller/metal/exceptions.rb b/actionpack/lib/action_controller/metal/exceptions.rb index 3c9d0c86a7..cf21d26752 100644 --- a/actionpack/lib/action_controller/metal/exceptions.rb +++ b/actionpack/lib/action_controller/metal/exceptions.rb @@ -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: diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 18cf5b2bb0..3de927abc8 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -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 diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index 26db91c741..e2964f9071 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -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({})