Exception: Chain::APIError

Inherits:
BaseError
  • Object
show all
Defined in:
lib/chain/errors.rb

Overview

APIError describes errors that are codified by the Chain API. They have an error code, a message, and an optional detail field that provides additional context for the error.

Direct Known Subclasses

UnauthorizedError

Constant Summary collapse

RETRIABLE_STATUS_CODES =
[
  408, # Request Timeout
  429, # Too Many Requests
  500, # Internal Server Error
  502, # Bad Gateway
  503, # Service Unavailable
  504, # Gateway Timeout
  509, # Bandwidth Limit Exceeded
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, response) ⇒ APIError

Returns a new instance of APIError.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chain/errors.rb', line 49

def initialize(body, response)
  self.code = body['code']
  self.chain_message = body['message']
  self.detail = body['detail']
  self.temporary = body['temporary']

  self.response = response
  self.request_id = response['Chain-Request-ID'] if response

  super self.class.format_error_message(code, chain_message, detail, request_id)
end

Instance Attribute Details

#chain_messageObject

Returns the value of attribute chain_message.



47
48
49
# File 'lib/chain/errors.rb', line 47

def chain_message
  @chain_message
end

#codeObject

Returns the value of attribute code.



47
48
49
# File 'lib/chain/errors.rb', line 47

def code
  @code
end

#dataObject

Returns the value of attribute data.



47
48
49
# File 'lib/chain/errors.rb', line 47

def data
  @data
end

#detailObject

Returns the value of attribute detail.



47
48
49
# File 'lib/chain/errors.rb', line 47

def detail
  @detail
end

#request_idObject

Returns the value of attribute request_id.



47
48
49
# File 'lib/chain/errors.rb', line 47

def request_id
  @request_id
end

#responseObject

Returns the value of attribute response.



47
48
49
# File 'lib/chain/errors.rb', line 47

def response
  @response
end

#temporaryObject

Returns the value of attribute temporary.



47
48
49
# File 'lib/chain/errors.rb', line 47

def temporary
  @temporary
end

Class Method Details

.format_error_message(code, message, detail, request_id) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/chain/errors.rb', line 65

def self.format_error_message(code, message, detail, request_id)
  tokens = []
  tokens << "Code: #{code}" if code.is_a?(String) && code.size > 0
  tokens << "Message: #{message}"
  tokens << "Detail: #{detail}" if detail.is_a?(String) && detail.size > 0
  tokens << "Request-ID: #{request_id}"
  tokens.join(' ')
end

Instance Method Details

#retriable?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chain/errors.rb', line 61

def retriable?
  temporary || (response && RETRIABLE_STATUS_CODES.include?(Integer(response.code)))
end