Exception: ChimeraHttpClient::Error

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error



6
7
8
9
10
11
12
# File 'lib/chimera_http_client/error.rb', line 6

def initialize(response)
  @body     = response.body
  @code     = response.code
  @time     = response.options&.fetch(:total_time, nil)
  @response = response # contains the request
  super
end

Instance Attribute Details

#bodyObject (readonly) Also known as: message

Returns the value of attribute body.



3
4
5
# File 'lib/chimera_http_client/error.rb', line 3

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/chimera_http_client/error.rb', line 3

def code
  @code
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/chimera_http_client/error.rb', line 3

def response
  @response
end

#timeObject (readonly)

Returns the value of attribute time.



3
4
5
# File 'lib/chimera_http_client/error.rb', line 3

def time
  @time
end

Instance Method Details

#error?Boolean



14
15
16
# File 'lib/chimera_http_client/error.rb', line 14

def error?
  true
end

#parsed_bodyObject



18
19
20
21
22
# File 'lib/chimera_http_client/error.rb', line 18

def parsed_body
  JSON.parse(body)
rescue JSON::ParserError
  { "non_json_body" => body }
end

#to_jsonObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chimera_http_client/error.rb', line 32

def to_json
  error = {
    code:        code,
    error_class: self.class.name,
    method:      http_method,
    url:         url,
    message:     message,
  }

  return error.merge({ request: response.request.inspect }).to_json if log_requests?

  error.to_json
end

#to_sObject



24
25
26
27
28
29
30
# File 'lib/chimera_http_client/error.rb', line 24

def to_s
  error = "#{self.class.name} (#{code}) #{message}, URL: #{url}"

  return "#{error}, Request: #{response.request.inspect}" if log_requests?

  error
end