Exception: PatientHttp::HttpError
- Defined in:
- lib/patient_http/http_error.rb
Overview
Error raised when an HTTP request receives a non-2xx response status code and the raise_error_responses option is enabled.
This error includes the full Response object so you can access the status code, headers, body, and other response data.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#response ⇒ Response
readonly
The HTTP response that triggered the error.
Class Method Summary collapse
-
.load(hash) ⇒ HttpError
Reconstruct an HttpError from a hash.
-
.new(response) ⇒ HttpError, ...
Create a new HttpError (or subclass) from a response.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Convert to hash with string keys for serialization.
- #callback_args ⇒ Object
- #duration ⇒ Object
- #error_class ⇒ Object
-
#error_type ⇒ Symbol
Returns the error type symbol.
- #http_method ⇒ Object
-
#initialize(response) ⇒ HttpError
constructor
Initializes a new HttpError.
- #request_id ⇒ Object
-
#status ⇒ Integer
Delegate common response methods for convenience.
- #url ⇒ Object
Methods inherited from Error
Constructor Details
#initialize(response) ⇒ HttpError
Initializes a new HttpError.
44 45 46 47 |
# File 'lib/patient_http/http_error.rb', line 44 def initialize(response) super("HTTP #{response.status} response from #{response.http_method.to_s.upcase} #{response.url}") @response = response end |
Instance Attribute Details
#response ⇒ Response (readonly)
Returns The HTTP response that triggered the error.
11 12 13 |
# File 'lib/patient_http/http_error.rb', line 11 def response @response end |
Class Method Details
.load(hash) ⇒ HttpError
Reconstruct an HttpError from a hash
35 36 37 38 |
# File 'lib/patient_http/http_error.rb', line 35 def load(hash) response = Response.load(hash["response"]) new(response) end |
.new(response) ⇒ HttpError, ...
Create a new HttpError (or subclass) from a response.
Returns ClientError for 4xx responses, ServerError for 5xx responses, or HttpError for other non-2xx responses.
21 22 23 24 25 26 27 28 29 |
# File 'lib/patient_http/http_error.rb', line 21 def new(response) if response.client_error? ClientError.allocate.tap { |error| error.send(:initialize, response) } elsif response.server_error? ServerError.allocate.tap { |error| error.send(:initialize, response) } else super end end |
Instance Method Details
#as_json ⇒ Hash
Convert to hash with string keys for serialization
90 91 92 93 94 |
# File 'lib/patient_http/http_error.rb', line 90 def as_json { "response" => @response.as_json } end |
#callback_args ⇒ Object
83 84 85 |
# File 'lib/patient_http/http_error.rb', line 83 def callback_args response.callback_args end |
#duration ⇒ Object
71 72 73 |
# File 'lib/patient_http/http_error.rb', line 71 def duration response.duration end |
#error_class ⇒ Object
79 80 81 |
# File 'lib/patient_http/http_error.rb', line 79 def error_class self.class end |
#error_type ⇒ Symbol
Returns the error type symbol. Provided for compatibility with RequestError.
59 60 61 |
# File 'lib/patient_http/http_error.rb', line 59 def error_type :http_error end |
#http_method ⇒ Object
67 68 69 |
# File 'lib/patient_http/http_error.rb', line 67 def http_method response.http_method end |
#request_id ⇒ Object
75 76 77 |
# File 'lib/patient_http/http_error.rb', line 75 def request_id response.request_id end |
#status ⇒ Integer
Delegate common response methods for convenience.
52 53 54 |
# File 'lib/patient_http/http_error.rb', line 52 def status @response.status end |
#url ⇒ Object
63 64 65 |
# File 'lib/patient_http/http_error.rb', line 63 def url response.url end |