Exception: PatientHttp::RequestError

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Error

#to_json

Constructor Details

#initialize(class_name:, message:, backtrace:, error_type:, duration:, request_id:, url:, http_method:, callback_args: nil) ⇒ RequestError

Initializes a new RequestError.

Parameters:

  • class_name (String)

    Name of the exception class

  • message (String)

    Exception message

  • backtrace (Array<String>)

    Exception backtrace

  • error_type (Symbol)

    Categorized error type

  • duration (Float)

    Request duration in seconds

  • request_id (String)

    Unique request identifier

  • url (String)

    Request URL

  • http_method (Symbol, String)

    HTTP method

  • callback_args (Hash, nil) (defaults to: nil)

    callback arguments (string keys)



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(message)
  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

#durationFloat (readonly)

Returns Request duration in seconds.

Returns:

  • (Float)

    Request duration in seconds



20
21
22
# File 'lib/patient_http/request_error.rb', line 20

def duration
  @duration
end

#error_typeSymbol (readonly)

of the error (e.g., :connection is used to group IO and socket errors).

Returns:

  • (Symbol)

    Categorized error type. This provides a higher level categorization



27
28
29
# File 'lib/patient_http/request_error.rb', line 27

def error_type
  @error_type
end

#http_methodSymbol (readonly)

Returns HTTP method.

Returns:

  • (Symbol)

    HTTP method



17
18
19
# File 'lib/patient_http/request_error.rb', line 17

def http_method
  @http_method
end

#request_idString (readonly)

Returns Unique request identifier.

Returns:

  • (String)

    Unique request identifier



23
24
25
# File 'lib/patient_http/request_error.rb', line 23

def request_id
  @request_id
end

#urlString (readonly)

Returns Request URL.

Returns:

  • (String)

    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.

Parameters:

  • exception (Exception)

    the exception to categorize

Returns:

  • (Symbol)

    the error type



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

Parameters:

  • exception (Exception)

    the exception to convert

  • duration (Float)

    request duration in seconds

  • request_id (String)

    the request ID

  • url (String)

    the request URL

  • http_method (Symbol, String)

    the HTTP method

  • callback_args (Hash, nil) (defaults to: nil)

    callback arguments (string keys)

Returns:



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.message,
    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

Parameters:

  • hash (Hash)

    hash representation

Returns:



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_jsonHash

Convert to hash with string keys for serialization

Returns:

  • (Hash)

    hash representation



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" => 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_argsCallbackArgs

Returns the callback arguments as a CallbackArgs object.

Returns:



146
147
148
# File 'lib/patient_http/request_error.rb', line 146

def callback_args
  @callback_args ||= CallbackArgs.load(@callback_args_data)
end

#error_classClass?

Get the actual Exception class constant from the class_name

Returns:

  • (Class, nil)

    the exception class or nil if not found



139
140
141
# File 'lib/patient_http/request_error.rb', line 139

def error_class
  ClassHelper.resolve_class_name(@class_name)
end