summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-02-12 10:32:18 -0800
committerdanielsdeleo <dan@opscode.com>2013-02-12 11:03:09 -0800
commit7c748c2f01487d199c86a3333e33cbf438443959 (patch)
treec95ad1b78ba78e1a302c1a56a7983c9cc7f6ab42
parent2e3e89fa0dc3969e57cc73af256a619cd4680a55 (diff)
downloadchef-7c748c2f01487d199c86a3333e33cbf438443959.tar.gz
fix json_compat to raise same errors as json gem
-rw-r--r--chef/lib/chef/json_compat.rb8
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