Exception: PatientHttp::RequestError
- Defined in:
- lib/patient_http/request_error.rb
Overview
Error object representing an exception from making an HTTP request. Note that this is not for HTTP error responses (4xx/5xx), but from actual exceptions raised during the request (timeouts, connection errors, SSL errors, etc).
This is how errors are passed back to the error continuation jobs for processing.
Constant Summary collapse
- ERROR_TYPES =
Valid error types
[:timeout, :connection, :ssl, :response_too_large, :unknown].freeze
Instance Attribute Summary collapse
-
#duration ⇒ Float
readonly
Request duration in seconds.
-
#error_type ⇒ Symbol
readonly
of the error (e.g., :connection is used to group IO and socket errors).
-
#http_method ⇒ Symbol
readonly
HTTP method.
-
#request_id ⇒ String
readonly
Unique request identifier.
-
#url ⇒ String
readonly
Request URL.
Class Method Summary collapse
-
.error_type(exception) ⇒ Symbol
Determine error type from exception.
-
.from_exception(exception, duration:, request_id:, url:, http_method:, callback_args: nil) ⇒ RequestError
Create a RequestError from an exception using pattern matching.
-
.load(hash) ⇒ RequestError
Reconstruct a RequestError from a hash.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Convert to hash with string keys for serialization.
-
#callback_args ⇒ CallbackArgs
Returns the callback arguments as a CallbackArgs object.
-
#error_class ⇒ Class?
Get the actual Exception class constant from the class_name.
-
#initialize(class_name:, message:, backtrace:, error_type:, duration:, request_id:, url:, http_method:, callback_args: nil) ⇒ RequestError
constructor
Initializes a new RequestError.
Methods inherited from Error
Constructor Details
#initialize(class_name:, message:, backtrace:, error_type:, duration:, request_id:, url:, http_method:, callback_args: nil) ⇒ RequestError
Initializes a new RequestError.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/patient_http/request_error.rb', line 106 def initialize(class_name:, message:, backtrace:, error_type:, duration:, request_id:, url:, http_method:, callback_args: nil) super() set_backtrace(backtrace) @class_name = class_name @error_type = error_type @duration = duration @request_id = request_id @url = url @http_method = http_method&.to_sym @callback_args_data = callback_args || {} end |
Instance Attribute Details
#duration ⇒ Float (readonly)
Returns Request duration in seconds.
20 21 22 |
# File 'lib/patient_http/request_error.rb', line 20 def duration @duration end |
#error_type ⇒ Symbol (readonly)
of the error (e.g., :connection is used to group IO and socket errors).
27 28 29 |
# File 'lib/patient_http/request_error.rb', line 27 def error_type @error_type end |
#http_method ⇒ Symbol (readonly)
Returns HTTP method.
17 18 19 |
# File 'lib/patient_http/request_error.rb', line 17 def http_method @http_method end |
#request_id ⇒ String (readonly)
Returns Unique request identifier.
23 24 25 |
# File 'lib/patient_http/request_error.rb', line 23 def request_id @request_id end |
#url ⇒ String (readonly)
Returns Request URL.
14 15 16 |
# File 'lib/patient_http/request_error.rb', line 14 def url @url end |
Class Method Details
.error_type(exception) ⇒ Symbol
Determine error type from exception.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/patient_http/request_error.rb', line 77 def error_type(exception) case exception in Async::TimeoutError :timeout in OpenSSL::SSL::SSLError :ssl in Errno::ECONNREFUSED | Errno::ECONNRESET | Errno::EHOSTUNREACH | Errno::EPIPE | SocketError | IOError :connection else if exception.is_a?(PatientHttp::ResponseTooLargeError) :response_too_large else :unknown end end end |
.from_exception(exception, duration:, request_id:, url:, http_method:, callback_args: nil) ⇒ RequestError
Create a RequestError from an exception using pattern matching
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/patient_http/request_error.rb', line 57 def from_exception(exception, duration:, request_id:, url:, http_method:, callback_args: nil) type = error_type(exception) new( class_name: exception.class.name, message: exception., backtrace: exception.backtrace || [], request_id: request_id, error_type: type, duration: duration, url: url, http_method: http_method, callback_args: callback_args ) end |
.load(hash) ⇒ RequestError
Reconstruct a RequestError from a hash
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/patient_http/request_error.rb', line 34 def load(hash) new( class_name: hash["class_name"], message: hash["message"], backtrace: hash["backtrace"], request_id: hash["request_id"], error_type: hash["error_type"]&.to_sym, duration: hash["duration"], url: hash["url"], http_method: hash["http_method"], callback_args: hash["callback_args"] ) end |
Instance Method Details
#as_json ⇒ Hash
Convert to hash with string keys for serialization
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/patient_http/request_error.rb', line 122 def as_json { "class_name" => @class_name, "message" => , "backtrace" => backtrace, "request_id" => request_id, "error_type" => error_type.to_s, "duration" => duration, "url" => url, "http_method" => http_method.to_s, "callback_args" => @callback_args_data } end |
#callback_args ⇒ CallbackArgs
Returns the callback arguments as a CallbackArgs object.
146 147 148 |
# File 'lib/patient_http/request_error.rb', line 146 def callback_args @callback_args ||= CallbackArgs.load(@callback_args_data) end |
#error_class ⇒ Class?
Get the actual Exception class constant from the class_name
139 140 141 |
# File 'lib/patient_http/request_error.rb', line 139 def error_class ClassHelper.resolve_class_name(@class_name) end |