diff options
author | danielsdeleo <dan@opscode.com> | 2013-02-12 10:32:18 -0800 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2013-02-12 10:32:18 -0800 |
commit | d136af9073698270d8b92b636a6b519f16ecc0ca (patch) | |
tree | 8364824002cb9fd76e489fa88e9a8b341dab5227 | |
parent | 53b2d013205966a1ade6213d014f6f1c301e1dbb (diff) | |
download | chef-d136af9073698270d8b92b636a6b519f16ecc0ca.tar.gz |
fix json_compat to raise same errors as json gem
-rw-r--r-- | chef/lib/chef/json_compat.rb | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/chef/lib/chef/json_compat.rb b/chef/lib/chef/json_compat.rb index e7c0a0e676..a377f3f419 100644 --- a/chef/lib/chef/json_compat.rb +++ b/chef/lib/chef/json_compat.rb @@ -54,6 +54,10 @@ class Chef def from_json(source, opts = {}) obj = ::Yajl::Parser.parse(source) + unless obj.kind_of?(Hash) || obj.kind_of?(Array) + raise JSON::ParserError, "Top level JSON object must be a Hash or Array (actual: #{obj.class})" + end + # The old default in the json gem (which we are mimicing because we # sadly rely on this misfeature) is to "create additions" i.e., convert # JSON objects into ruby objects. Explicit :create_additions => false @@ -63,6 +67,8 @@ class Chef else obj end + rescue Yajl::ParseError => e + raise JSON::ParserError, e.message end # Look at an object that's a basic type (from json parse) and convert it @@ -127,7 +133,7 @@ class Chef when /^Chef::Resource/ Chef::Resource.find_subclass_by_name(json_class) else - raise ArgumentError, "Unsupported `json_class` type '#{json_class}'" + raise JSON::ParserError, "Unsupported `json_class` type '#{json_class}'" end end |