bolshakov / stoplight

:traffic_light: Traffic control for code.
http://bolshakov.github.io/stoplight/
MIT License
384 stars 40 forks source link

Restoring failures from JSON doesn't always work #42

Closed tfausak closed 10 years ago

tfausak commented 10 years ago

It's possible that the error class in the JSON isn't in scope when converting from JSON.

>> Stoplight::Failure.from_json('{ "error": "#<CustomError: message>", "time": "2001-02-03T04:05:06Z" }')
NameError: uninitialized constant CustomError
jpowell commented 10 years ago

Should the error be in a serialized format?

tfausak commented 10 years ago

It is, in a way. The problem is that #inspect doesn't always give you enough information to rebuild the exception.

tfausak commented 10 years ago

Here's a more complete failing example:

>> CustomError = Class.new(StandardError)
=> CustomError
>> json = Stoplight::Failure.new(CustomError.new('custom message')).to_json
=> "{\"error\":\"#<CustomError: custom message>\",\"time\":\"2014-09-12 11:42:31 -0500\"}"
>> Object.send(:remove_const, :CustomError)
=> CustomError
>> Stoplight::Failure.from_json(json)
NameError: uninitialized constant CustomError