diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-09-26 10:55:29 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-09-26 10:55:29 -0700 |
commit | 8156178894dc6c57aca50a4d5f6c9398beb5f3e0 (patch) | |
tree | 485ef273dfeb2c610a0225d0261ef95eb65c5045 /actionpack | |
parent | d36f57f430c011e889c7d45335beea4ea0dc9769 (diff) | |
parent | fd99bb892652b1ffffdd7f9271184235d6040111 (diff) | |
download | rails-8156178894dc6c57aca50a4d5f6c9398beb5f3e0.tar.gz rails-8156178894dc6c57aca50a4d5f6c9398beb5f3e0.tar.bz2 rails-8156178894dc6c57aca50a4d5f6c9398beb5f3e0.zip |
Merge pull request #7444 from szimek/params_parser_raises_parsing_error
Raise generic ParseError exception when ParamsParser fails parsing request params
Diffstat (limited to 'actionpack')
3 files changed, 18 insertions, 7 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 1cb803ffb9..2c98ca03a8 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -4,6 +4,15 @@ require 'active_support/core_ext/hash/indifferent_access' module ActionDispatch class ParamsParser + class ParseError < StandardError + attr_reader :original_exception + + def initialize(message, original_exception) + super(message) + @original_exception = original_exception + end + end + DEFAULT_PARSERS = { Mime::XML => :xml_simple, Mime::JSON => :json @@ -38,14 +47,12 @@ module ActionDispatch when Proc strategy.call(request.raw_post) when :xml_simple, :xml_node - data = Hash.from_xml(request.body.read) || {} - request.body.rewind if request.body.respond_to?(:rewind) + data = Hash.from_xml(request.raw_post) || {} data.with_indifferent_access when :yaml YAML.load(request.raw_post) when :json - data = ActiveSupport::JSON.decode(request.body) - request.body.rewind if request.body.respond_to?(:rewind) + data = ActiveSupport::JSON.decode(request.raw_post) data = {:_json => data} unless data.is_a?(Hash) data.with_indifferent_access else @@ -54,7 +61,7 @@ module ActionDispatch rescue Exception => e # YAML, XML or Ruby code block errors logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}" - raise e + raise ParseError.new(e.message, e) end def content_type_from_legacy_post_data_format_header(env) diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index 302bff0696..c0c3147e37 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -46,7 +46,9 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log json = "[\"person]\": {\"name\": \"David\"}}" - assert_raise(MultiJson::DecodeError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } + exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} } + assert_equal MultiJson::DecodeError, exception.original_exception.class + assert_equal exception.original_exception.message, exception.message ensure $stderr = STDERR end diff --git a/actionpack/test/dispatch/request/xml_params_parsing_test.rb b/actionpack/test/dispatch/request/xml_params_parsing_test.rb index 84823e2896..cb68667002 100644 --- a/actionpack/test/dispatch/request/xml_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/xml_params_parsing_test.rb @@ -68,7 +68,9 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest begin $stderr = StringIO.new # suppress the log xml = "<person><name>David</name></pineapple>" - assert_raise(REXML::ParseException) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } + exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) } + assert_equal REXML::ParseException, exception.original_exception.class + assert_equal exception.original_exception.message, exception.message ensure $stderr = STDERR end |