Permalink
Browse files

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

  • Loading branch information...
1 parent facd3e8 commit cde326bcefa7e842d5f8090d24b700399ac3d705 @tenderlove tenderlove committed Oct 25, 2012
@@ -3,6 +3,13 @@ class ActionControllerError < StandardError #:nodoc:
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:
@@ -253,15 +253,15 @@ def session_options=(options)
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
# Override Rack's POST method to support indifferent access
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
@@ -560,6 +560,20 @@ def url_for(options = {})
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({})

0 comments on commit cde326b

Please sign in to comment.