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, options = {}) ⇒ Error

Returns a new instance of Error.



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

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

  @deserializer = options[:deserializer][:error]

  super(response.body)
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

#deserializerObject (readonly)

Returns the value of attribute deserializer.



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

def deserializer
  @deserializer
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

Returns:

  • (Boolean)


17
18
19
# File 'lib/chimera_http_client/error.rb', line 17

def error?
  true
end

#parsed_bodyObject



21
22
23
# File 'lib/chimera_http_client/error.rb', line 21

def parsed_body
  deserializer.call(body)
end

#to_json(_options = {}) ⇒ Object



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

def to_json(_options = {})
  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



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

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

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

  error
end