Exception: PatientHttp::HttpError

Inherits:
Error
  • Object
show all
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

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Error

#to_json

Constructor Details

#initialize(response) ⇒ HttpError

Initializes a new HttpError.

Parameters:

  • response (Response)

    The HTTP response with non-2xx status code



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

#responseResponse (readonly)

Returns The HTTP response that triggered the error.

Returns:

  • (Response)

    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

Parameters:

  • hash (Hash)

    hash representation

Returns:



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.

Parameters:

  • response (Response)

    The HTTP response with non-2xx status code

Returns:



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_jsonHash

Convert to hash with string keys for serialization

Returns:

  • (Hash)

    hash representation



90
91
92
93
94
# File 'lib/patient_http/http_error.rb', line 90

def as_json
  {
    "response" => @response.as_json
  }
end

#callback_argsObject



83
84
85
# File 'lib/patient_http/http_error.rb', line 83

def callback_args
  response.callback_args
end

#durationObject



71
72
73
# File 'lib/patient_http/http_error.rb', line 71

def duration
  response.duration
end

#error_classObject



79
80
81
# File 'lib/patient_http/http_error.rb', line 79

def error_class
  self.class
end

#error_typeSymbol

Returns the error type symbol. Provided for compatibility with RequestError.

Returns:

  • (Symbol)

    the error type



59
60
61
# File 'lib/patient_http/http_error.rb', line 59

def error_type
  :http_error
end

#http_methodObject



67
68
69
# File 'lib/patient_http/http_error.rb', line 67

def http_method
  response.http_method
end

#request_idObject



75
76
77
# File 'lib/patient_http/http_error.rb', line 75

def request_id
  response.request_id
end

#statusInteger

Delegate common response methods for convenience.

Returns:

  • (Integer)

    HTTP status code



52
53
54
# File 'lib/patient_http/http_error.rb', line 52

def status
  @response.status
end

#urlObject



63
64
65
# File 'lib/patient_http/http_error.rb', line 63

def url
  response.url
end