Exception: Johac::Error::ResponseError
- Inherits:
-
Johac::Error
- Object
- StandardError
- Johac::Error
- Johac::Error::ResponseError
- Defined in:
- lib/johac/error.rb
Overview
Exception to be used when dealing with HTTP responses from a Johac API.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Class Method Summary collapse
-
.from_response(status_code, headers, body) ⇒ Johac::Error::RetryableError, ...
If a problem is detected in an HTTP response, build the proper exception, otherwise return nil.
Instance Method Summary collapse
-
#initialize(code, body, headers) ⇒ ResponseError
constructor
Will attempt to decode a response body via JSON, and look for the ‘message’ key in the resulting (assumed) hash.
Constructor Details
#initialize(code, body, headers) ⇒ ResponseError
Will attempt to decode a response body via JSON, and look for the ‘message’ key in the resulting (assumed) hash. If response body cannot be parsed via JSON the entire response body is set as the message for the exception.
18 19 20 21 22 23 24 25 26 |
# File 'lib/johac/error.rb', line 18 def initialize(code, body, headers) @code = code @body = if headers['Content-Type'] == 'application/json' JSON.parse(body) rescue body else body end super(body) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
12 13 14 |
# File 'lib/johac/error.rb', line 12 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
11 12 13 |
# File 'lib/johac/error.rb', line 11 def code @code end |
Class Method Details
.from_response(status_code, headers, body) ⇒ Johac::Error::RetryableError, ...
If a problem is detected in an HTTP response, build the proper exception, otherwise return nil.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/johac/error.rb', line 38 def self.from_response(status_code, headers, body) if klass = case status_code when 429 then RetryableError when 503 then RetryableError when 504 then RetryableError when 400..499 then ClientError when 500..599 then ServerError end klass.new(status_code, body, headers) end end |