Permalink
Browse files

Rename LoadError to ParseError

This should help better distinguish parsing errors from native LoadError
  • Loading branch information...
rwz committed Mar 5, 2014
1 parent 4da612b commit 4abb98fe3a90b2a7b3d1594515c8a06042b4a27d
Showing with 46 additions and 23 deletions.
  1. +2 −2 lib/multi_json.rb
  2. +17 −0 lib/multi_json/parse_error.rb
  3. +1 −7 spec/multi_json_spec.rb
  4. +17 −13 spec/shared/adapter.rb
  5. +9 −1 spec/spec_helper.rb
View
@@ -1,7 +1,7 @@
require 'multi_json/options'
require 'multi_json/version'
require 'multi_json/adapter_error'
-require 'multi_json/load_error'
+require 'multi_json/parse_error'
module MultiJson
include Options
@@ -120,7 +120,7 @@ def load(string, options={})
begin
adapter.load(string, options)
rescue adapter::ParseError => exception
- raise LoadError.new(exception.message, exception.backtrace, string)
+ raise ParseError.build(exception, string)
end
end
alias decode load
@@ -0,0 +1,17 @@
+module MultiJson
+ class ParseError < StandardError
+ attr_reader :data, :cause
+
+ def self.build(original_exception, data)
+ new(original_exception.message).tap do |exception|
+ exception.instance_eval do
+ @cause = original_exception
+ set_backtrace original_exception.backtrace
+ @data = data
+ end
+ end
+ end
+ end
+
+ DecodeError = LoadError = ParseError # Legacy support
+end
View
@@ -94,13 +94,7 @@
end
it 'gives access to original error when raising AdapterError' do
- exception = nil
- begin
- MultiJson.use 'foobar'
- rescue MultiJson::AdapterError => e
- exception = e
- end
-
+ exception = get_exception(MultiJson::AdapterError){ MultiJson.use 'foobar' }
expect(exception.cause).to be_instance_of(::LoadError)
expect(exception.message).to include("-- multi_json/adapters/foobar")
expect(exception.message).to include("Did not recognize your adapter specification")
View
@@ -162,35 +162,39 @@ def to_json(*)
expect(MultiJson.load('{"abc":"def"}')).to eq('abc' => 'def')
end
- it 'raises MultiJson::LoadError on blank input or invalid input' do
+ it 'raises MultiJson::ParseError on blank input or invalid input' do
[nil, '{"abc"}', ' ', "\t\t\t", "\n", "\x82\xAC\xEF"].each do |input|
if input == "\x82\xAC\xEF"
pending 'GSON bug: https://github.com/avsej/gson.rb/issues/3' if adapter.name =~ /Gson/
pending 'JrJackson bug: https://github.com/guyboertje/jrjackson/issues/21' if adapter.name =~ /JrJackson/
end
- expect{MultiJson.load(input)}.to raise_error(MultiJson::LoadError)
+ expect{MultiJson.load(input)}.to raise_error(MultiJson::ParseError)
end
end
- it 'raises MultiJson::LoadError with data on invalid JSON' do
+ it 'raises MultiJson::ParseError with data on invalid JSON' do
data = '{invalid}'
- begin
- MultiJson.load(data)
- rescue MultiJson::LoadError => le
- expect(le.data).to eq(data)
- end
+ exception = get_exception(MultiJson::ParseError){ MultiJson.load data }
+ expect(exception.data).to eq(data)
+ expect(exception.cause).to be_kind_of(MultiJson.adapter::ParseError)
end
it 'catches MultiJson::DecodeError for legacy support' do
data = '{invalid}'
- begin
- MultiJson.load(data)
- rescue MultiJson::DecodeError => de
- expect(de.data).to eq(data)
- end
+ exception = get_exception(MultiJson::DecodeError){ MultiJson.load data }
+ expect(exception.data).to eq(data)
+ expect(exception.cause).to be_kind_of(MultiJson.adapter::ParseError)
+ end
+
+ it 'catches MultiJson::LoadError for legacy support' do
+ data = '{invalid}'
+ exception = get_exception(MultiJson::LoadError){ MultiJson.load data }
+ expect(exception.data).to eq(data)
+ expect(exception.cause).to be_kind_of(MultiJson.adapter::ParseError)
end
+
it 'stringifys symbol keys when encoding' do
dumped_json = MultiJson.dump(:a => 1, :b => {:c => 2})
loaded_json = MultiJson.load(dumped_json)
View
@@ -70,4 +70,12 @@ def simulate_no_adapters
yield
end
end
-end
+end
+
+def get_exception(exception_class = StandardError)
+ begin
+ yield
+ rescue exception_class => exception
+ exception
+ end
+end

0 comments on commit 4abb98f

Please sign in to comment.