diff options
author | Rob <rob.anderton@thewebfellas.com> | 2009-03-18 11:28:41 +0000 |
---|---|---|
committer | Michael Fellinger <m.fellinger@gmail.com> | 2009-03-26 14:58:58 +0900 |
commit | 880d9be0417175a89e57b533bfa355bbff7c3845 (patch) | |
tree | 857ffe5316bb2bc8de82dfcbe8e051ae2a71cd94 | |
parent | 24d33c630fb575f1f63a9ad40009f12b90a15c6a (diff) | |
download | rack-880d9be0417175a89e57b533bfa355bbff7c3845.tar.gz |
Added extra nested params error handling and added messages to exceptions to help with debugging
Signed-off-by: Michael Fellinger <m.fellinger@gmail.com>
-rw-r--r-- | lib/rack/utils.rb | 5 | ||||
-rw-r--r-- | test/spec_rack_utils.rb | 13 |
2 files changed, 14 insertions, 4 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 0a61bce7..7acc10d2 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -73,12 +73,12 @@ module Rack params[k] = v elsif after == "[]" params[k] ||= [] - raise TypeError unless params[k].is_a?(Array) + raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) params[k] << v elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$) child_key = $1 params[k] ||= [] - raise TypeError unless params[k].is_a?(Array) + raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key) normalize_params(params[k].last, child_key, v) else @@ -86,6 +86,7 @@ module Rack end else params[k] ||= {} + raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash) params[k] = normalize_params(params[k], after, v) end diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb index 9bd89048..2ac563b2 100644 --- a/test/spec_rack_utils.rb +++ b/test/spec_rack_utils.rb @@ -93,8 +93,17 @@ context "Rack::Utils" do Rack::Utils.parse_nested_query("x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3"). should.equal "x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]} - lambda { Rack::Utils.parse_nested_query("foo[bar]=1&foo[]=1") }. - should.raise TypeError + lambda { Rack::Utils.parse_nested_query("x[y]=1&x[y]z=2") }. + should.raise(TypeError). + message.should.equal "expected Hash (got String) for param `y'" + + lambda { Rack::Utils.parse_nested_query("x[y]=1&x[]=1") }. + should.raise(TypeError). + message.should.equal "expected Array (got Hash) for param `x'" + + lambda { Rack::Utils.parse_nested_query("x[y]=1&x[y][][w]=2") }. + should.raise(TypeError). + message.should.equal "expected Array (got String) for param `y'" end specify "should build query strings correctly" do |