Exception: NxtHttpClient::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/nxt_http_client.rb,
lib/nxt_http_client/error.rb

Defined Under Namespace

Classes: BadRequest, CertificateError, ClientError, ConnectionFailed, Forbidden, NameResolutionError, NetworkError, NotFound, ServerError, Timeout, TlsError, TooManyRequests, Unauthorized, UnprocessableEntity

Constant Summary collapse

CERTIFICATE_RETURN_CODES =

Cert/trust failures — mapped to non-retryable CertificateError, kept out of the generic TlsError.

%i[
  peer_failed_verification ssl_certproblem ssl_cacert_badfile ssl_issuer_error ssl_crl_badfile
].freeze
REDACTED =
'[REDACTED]'
SENSITIVE_HEADERS =
%w[Authorization Proxy-Authorization].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, message = nil) ⇒ Error

Returns a new instance of Error.



47
48
49
50
51
52
53
# File 'lib/nxt_http_client/error.rb', line 47

def initialize(response, message = nil)
  @response = response.blank? ? Typhoeus::Response.new : response
  @id = SecureRandom.uuid
  @message = message || default_message

  super(@message)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



55
56
57
# File 'lib/nxt_http_client/error.rb', line 55

def id
  @id
end

#messageObject (readonly) Also known as: to_s

Returns the value of attribute message.



55
56
57
# File 'lib/nxt_http_client/error.rb', line 55

def message
  @message
end

#responseObject (readonly)

Returns the value of attribute response.



55
56
57
# File 'lib/nxt_http_client/error.rb', line 55

def response
  @response
end

Class Method Details

.error_class_for(response) ⇒ Object



15
16
17
18
19
20
# File 'lib/nxt_http_client/error.rb', line 15

def self.error_class_for(response)
  code = response.respond_to?(:code) ? response.code.to_i : 0
  return network_error_class(response.respond_to?(:return_code) ? response.return_code : nil) if code.zero?

  status_error_class(code)
end

.from_response(response, message = nil) ⇒ Object



11
12
13
# File 'lib/nxt_http_client/error.rb', line 11

def self.from_response(response, message = nil)
  error_class_for(response).new(response, message)
end

.network_error_class(return_code) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/nxt_http_client/error.rb', line 36

def self.network_error_class(return_code)
  case return_code
  when :operation_timedout then Timeout
  when :couldnt_connect then ConnectionFailed
  when :couldnt_resolve_host, :couldnt_resolve_proxy then NameResolutionError
  when *CERTIFICATE_RETURN_CODES then CertificateError
  else
    return_code.to_s.include?('ssl') ? TlsError : NetworkError
  end
end

.status_error_class(code) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nxt_http_client/error.rb', line 22

def self.status_error_class(code)
  case code
  when 400 then BadRequest
  when 401 then Unauthorized
  when 403 then Forbidden
  when 404 then NotFound
  when 422 then UnprocessableEntity
  when 429 then TooManyRequests
  when 400..499 then ClientError
  when 500..599 then ServerError
  else self # 3xx etc. → base Error
  end
end

Instance Method Details

#bodyObject



77
78
79
80
81
82
83
84
85
# File 'lib/nxt_http_client/error.rb', line 77

def body
  if response_content_type&.starts_with?(ApplicationJson)
    JSON.parse(response.body)
  else
    response.body
  end
rescue ::JSON::JSONError
  response.body
end

#default_messageObject



60
61
62
# File 'lib/nxt_http_client/error.rb', line 60

def default_message
  "#{self.class.name}::#{response_code}"
end

#requestObject



93
94
95
# File 'lib/nxt_http_client/error.rb', line 93

def request
  @request ||= response.request || Typhoeus::Request.new('/dev/null', {})
end

#request_headersObject



109
110
111
# File 'lib/nxt_http_client/error.rb', line 109

def request_headers
  @request_headers ||= (request.original_options[:headers] || {}).with_indifferent_access
end

#request_optionsObject



105
106
107
# File 'lib/nxt_http_client/error.rb', line 105

def request_options
  @request_options ||= (request.original_options || {}).with_indifferent_access
end

#response_codeObject



87
88
89
90
91
# File 'lib/nxt_http_client/error.rb', line 87

def response_code
  return response.code if response.respond_to?(:code)
  
  0
end

#response_content_typeObject



121
122
123
# File 'lib/nxt_http_client/error.rb', line 121

def response_content_type
  response_headers['Content-Type']
end

#response_headersObject



117
118
119
# File 'lib/nxt_http_client/error.rb', line 117

def response_headers
  @response_headers ||= (response.headers || {}).with_indifferent_access
end

#response_optionsObject



113
114
115
# File 'lib/nxt_http_client/error.rb', line 113

def response_options
  @response_options ||= (response.options || {}).with_indifferent_access
end

#to_hObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nxt_http_client/error.rb', line 64

def to_h
  {
    id: id,
    url: url,
    response_code: response_code,
    request_options: redact_credentials(request_options),
    response_headers: response_headers,
    request_headers: redact_authorization(request_headers),
    body: body,
    x_request_id: x_request_id
  }
end

#urlObject



97
98
99
# File 'lib/nxt_http_client/error.rb', line 97

def url
  request.url
end

#x_request_idObject



101
102
103
# File 'lib/nxt_http_client/error.rb', line 101

def x_request_id
  request_headers[XRequestId]
end