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



49
50
51
# File 'lib/nxt_http_client/error.rb', line 49

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

#request_headersObject



65
66
67
# File 'lib/nxt_http_client/error.rb', line 65

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

#request_optionsObject



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

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

#response_codeObject



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

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

#response_content_typeObject



77
78
79
# File 'lib/nxt_http_client/error.rb', line 77

def response_content_type
  response_headers['Content-Type']
end

#response_headersObject



73
74
75
# File 'lib/nxt_http_client/error.rb', line 73

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

#response_optionsObject



69
70
71
# File 'lib/nxt_http_client/error.rb', line 69

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



53
54
55
# File 'lib/nxt_http_client/error.rb', line 53

def url
  request.url
end

#x_request_idObject



57
58
59
# File 'lib/nxt_http_client/error.rb', line 57

def x_request_id
  request_headers[XRequestId]
end