Exception: Coinbase::APIError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/coinbase/errors.rb

Overview

A wrapper for API errors to provide more context.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(err, code: nil, message: nil, unhandled: false) ⇒ APIError

Initializes a new APIError object.

Parameters:

  • err (Coinbase::Client::APIError)

    The underlying error object.



13
14
15
16
17
18
19
20
# File 'lib/coinbase/errors.rb', line 13

def initialize(err, code: nil, message: nil, unhandled: false)
  @http_code = err.code
  @api_code = code
  @api_message = message
  @handled = code && message && !unhandled

  super(err)
end

Instance Attribute Details

#api_codeObject (readonly)

Returns the value of attribute api_code.



9
10
11
# File 'lib/coinbase/errors.rb', line 9

def api_code
  @api_code
end

#api_messageObject (readonly)

Returns the value of attribute api_message.



9
10
11
# File 'lib/coinbase/errors.rb', line 9

def api_message
  @api_message
end

#handledObject (readonly)

Returns the value of attribute handled.



9
10
11
# File 'lib/coinbase/errors.rb', line 9

def handled
  @handled
end

#http_codeObject (readonly)

Returns the value of attribute http_code.



9
10
11
# File 'lib/coinbase/errors.rb', line 9

def http_code
  @http_code
end

Class Method Details

.from_error(err) ⇒ APIError

Creates a specific APIError based on the API error code.

Parameters:

  • err (Coinbase::Client::APIError)

    The underlying error object.

Returns:

  • (APIError)

    The specific APIError object.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coinbase/errors.rb', line 25

def self.from_error(err)
  raise ArgumentError, 'Argument must be a Coinbase::Client::APIError' unless err.is_a? Coinbase::Client::ApiError
  return APIError.new(err) unless err.response_body

  begin
    body = JSON.parse(err.response_body)
  rescue JSON::ParserError
    return APIError.new(err)
  end

  message = body['message']
  code = body['code']

  if ERROR_CODE_TO_ERROR_CLASS.key?(code)
    ERROR_CODE_TO_ERROR_CLASS[code].new(err, code: code, message: message)
  else
    APIError.new(err, code: code, message: message, unhandled: true)
  end
end

Instance Method Details

#inspectObject



54
55
56
# File 'lib/coinbase/errors.rb', line 54

def inspect
  to_s
end

#to_sObject

Override to_s to display a friendly error message



46
47
48
49
50
51
52
# File 'lib/coinbase/errors.rb', line 46

def to_s
  # For handled errors, display just the API message as that provides sufficient context.
  return api_message if handled

  # For unhandled errors, display the full error message
  super
end