mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #7444 from szimek/params_parser_raises_parsing_error
Raise generic ParseError exception when ParamsParser fails parsing request params
This commit is contained in:
commit
8156178894
3 changed files with 18 additions and 7 deletions
|
@ -4,6 +4,15 @@ require 'active_support/core_ext/hash/indifferent_access'
|
||||||
|
|
||||||
module ActionDispatch
|
module ActionDispatch
|
||||||
class ParamsParser
|
class ParamsParser
|
||||||
|
class ParseError < StandardError
|
||||||
|
attr_reader :original_exception
|
||||||
|
|
||||||
|
def initialize(message, original_exception)
|
||||||
|
super(message)
|
||||||
|
@original_exception = original_exception
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
DEFAULT_PARSERS = {
|
DEFAULT_PARSERS = {
|
||||||
Mime::XML => :xml_simple,
|
Mime::XML => :xml_simple,
|
||||||
Mime::JSON => :json
|
Mime::JSON => :json
|
||||||
|
@ -38,14 +47,12 @@ module ActionDispatch
|
||||||
when Proc
|
when Proc
|
||||||
strategy.call(request.raw_post)
|
strategy.call(request.raw_post)
|
||||||
when :xml_simple, :xml_node
|
when :xml_simple, :xml_node
|
||||||
data = Hash.from_xml(request.body.read) || {}
|
data = Hash.from_xml(request.raw_post) || {}
|
||||||
request.body.rewind if request.body.respond_to?(:rewind)
|
|
||||||
data.with_indifferent_access
|
data.with_indifferent_access
|
||||||
when :yaml
|
when :yaml
|
||||||
YAML.load(request.raw_post)
|
YAML.load(request.raw_post)
|
||||||
when :json
|
when :json
|
||||||
data = ActiveSupport::JSON.decode(request.body)
|
data = ActiveSupport::JSON.decode(request.raw_post)
|
||||||
request.body.rewind if request.body.respond_to?(:rewind)
|
|
||||||
data = {:_json => data} unless data.is_a?(Hash)
|
data = {:_json => data} unless data.is_a?(Hash)
|
||||||
data.with_indifferent_access
|
data.with_indifferent_access
|
||||||
else
|
else
|
||||||
|
@ -54,7 +61,7 @@ module ActionDispatch
|
||||||
rescue Exception => e # YAML, XML or Ruby code block errors
|
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}"
|
logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"
|
||||||
|
|
||||||
raise e
|
raise ParseError.new(e.message, e)
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_type_from_legacy_post_data_format_header(env)
|
def content_type_from_legacy_post_data_format_header(env)
|
||||||
|
|
|
@ -46,7 +46,9 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest
|
||||||
begin
|
begin
|
||||||
$stderr = StringIO.new # suppress the log
|
$stderr = StringIO.new # suppress the log
|
||||||
json = "[\"person]\": {\"name\": \"David\"}}"
|
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
|
ensure
|
||||||
$stderr = STDERR
|
$stderr = STDERR
|
||||||
end
|
end
|
||||||
|
|
|
@ -68,7 +68,9 @@ class XmlParamsParsingTest < ActionDispatch::IntegrationTest
|
||||||
begin
|
begin
|
||||||
$stderr = StringIO.new # suppress the log
|
$stderr = StringIO.new # suppress the log
|
||||||
xml = "<person><name>David</name></pineapple>"
|
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
|
ensure
|
||||||
$stderr = STDERR
|
$stderr = STDERR
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue