Permalink
Browse files

Merge pull request #7444 from szimek/params_parser_raises_parsing_error

Raise generic ParseError exception when ParamsParser fails parsing request params
  • Loading branch information...
2 parents d36f57f + fd99bb8 commit 8156178894dc6c57aca50a4d5f6c9398beb5f3e0 @tenderlove tenderlove committed Sep 26, 2012
View
17 actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -4,6 +4,15 @@
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 @@ def parse_formatted_parameters(env)
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 @@ def parse_formatted_parameters(env)
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)
View
4 actionpack/test/dispatch/request/json_params_parsing_test.rb
@@ -46,7 +46,9 @@ def teardown
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
View
4 actionpack/test/dispatch/request/xml_params_parsing_test.rb
@@ -68,7 +68,9 @@ def call(env)
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

0 comments on commit 8156178

Please sign in to comment.