Exception: NxtHttpClient::Error

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, message = nil) ⇒ Error

Returns a new instance of Error.



3
4
5
6
7
8
9
# File 'lib/nxt_http_client/error.rb', line 3

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.



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

def id
  @id
end

#messageObject (readonly) Also known as: to_s

Returns the value of attribute message.



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

def message
  @message
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#bodyObject



33
34
35
36
37
38
39
40
41
# File 'lib/nxt_http_client/error.rb', line 33

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



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

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

#requestObject



47
48
49
# File 'lib/nxt_http_client/error.rb', line 47

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

#request_headersObject



63
64
65
# File 'lib/nxt_http_client/error.rb', line 63

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

#request_optionsObject



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

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

#response_codeObject



43
44
45
# File 'lib/nxt_http_client/error.rb', line 43

def response_code
  response.code || 0
end

#response_content_typeObject



75
76
77
# File 'lib/nxt_http_client/error.rb', line 75

def response_content_type
  response_headers['Content-Type']
end

#response_headersObject



71
72
73
# File 'lib/nxt_http_client/error.rb', line 71

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

#response_optionsObject



67
68
69
# File 'lib/nxt_http_client/error.rb', line 67

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

#to_hObject



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

def to_h
  {
    id: id,
    url: url,
    response_code: response_code,
    request_options: request_options,
    response_headers: response_headers,
    request_headers: request_headers,
    body: body,
    x_request_id: x_request_id
  }
end

#urlObject



51
52
53
# File 'lib/nxt_http_client/error.rb', line 51

def url
  request.url
end

#x_request_idObject



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

def x_request_id
  request_headers[XRequestId]
end