lib/hyper_resource/exceptions.rb in hyperresource-0.1.9.5 vs lib/hyper_resource/exceptions.rb in hyperresource-0.2.0
- old
+ new
@@ -1,28 +1,40 @@
class HyperResource
- class Exception < ::Exception
- attr_accessor :response # Response body which led to this
- attr_accessor :response_object # Response object which led to this
- attr_accessor :cause # Internal exception which led to this
+ class Exception < ::StandardError
+ ## The internal exception which led to this one, if any.
+ attr_accessor :cause
- def initialize(message, opts={})
- self.response = opts[:response]
- self.response_object = opts[:response_object]
- self.cause = opts[:cause]
+ def initialize(message, attrs={}) # @private
+ self.cause = attrs[:cause]
+ super(message)
+ end
+ end
+ class ResponseError < Exception
+ ## The +Faraday::Response+ object which led to this exception.
+ attr_accessor :response
+
+ ## The deserialized response body which led to this exception.
+ ## May be blank, e.g. in case of deserialization errors.
+ attr_accessor :body
+
+ def initialize(message, attrs={}) # @private
+ self.response = attrs[:response]
+ self.body = attrs[:body]
+
## Try to help out with the message
- if self.response_object
- if error = self.response_object['error']
+ if self.body
+ if error = self.body['error']
message = "#{message} (#{error})"
end
elsif self.response
message = "#{message} (\"#{self.response.inspect}\")"
end
- super(message)
+ super(message, attrs)
end
end
- class ResponseError < Exception; end
- class ClientError < Exception; end
- class ServerError < Exception; end
+ class ClientError < ResponseError; end
+ class ServerError < ResponseError; end
end
+