Module: CoinbaseCommerceClient::Errors

Defined in:
lib/coinbase_commerce_client/api_errors.rb

Defined Under Namespace

Classes: APIConnectionError, APIError, AuthenticationError, BadRequestError, InternalServerError, InvalidRequestError, ParamRequiredError, RateLimitExceededError, ResourceNotFoundError, ServiceUnavailableError, SignatureVerificationError, ValidationError, WebhookError, WebhookInvalidPayload

Class Method Summary collapse

Class Method Details

.general_api_error(status, body) ⇒ Object



95
96
97
98
# File 'lib/coinbase_commerce_client/api_errors.rb', line 95

def self.general_api_error(status, body)
  APIError.new("Invalid response object from API: #{body.inspect} " \
                 "(HTTP response code: #{status} http_body: #{body}")
end

.handle_error_response(http_resp) ⇒ Object

Errors handling



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/coinbase_commerce_client/api_errors.rb', line 81

def self.handle_error_response(http_resp)
  begin
    resp = CoinbaseCommerceClientResponse.from_faraday_hash(http_resp)
    error_data = resp.data[:error]

    raise APIError, 'Unknown error' unless error_data
  rescue JSON::ParserError, APIError
    raise general_api_error(http_resp[:status], http_resp[:body])
  end
  error = specific_api_error(resp, error_data)
  error.response = resp
  raise(error)
end

.handle_network_error(e, api_base = nil) ⇒ Object

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coinbase_commerce_client/api_errors.rb', line 135

def self.handle_network_error(e, api_base = nil)
  api_base ||= @api_uri
  message = case e
            when Faraday::ConnectionFailed
              'Unexpected error communicating when trying to connect to Coinbase Commerce.'
            when Faraday::SSLError
              'Could not establish a secure connection to Coinbase Commerce.'
            when Faraday::TimeoutError
              "Could not connect to Coinbase Commerce (#{api_base})."
            else
              'Unexpected error communicating with Coinbase Commerce.'
            end
  raise APIConnectionError, message + "\n\n(Network error: #{e.message})"
end

.specific_api_error(resp, error_data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/coinbase_commerce_client/api_errors.rb', line 100

def self.specific_api_error(resp, error_data)
  opts = {
    http_body: resp.http_body,
    http_headers: resp.http_headers,
    http_status: resp.http_status,
    json_body: resp.data
  }
  case resp.http_status
  when 400
    # in case of known error code
    case error_data[:type]
    when 'param_required'
      ParamRequiredError.new(error_data[:message], opts)
    when 'validation_error'
      ValidationError.new(error_data[:message], opts)
    when 'invalid_request'
      InvalidRequestError.new(error_data[:message], opts)
    else
      InvalidRequestError.new(error_data[:message], opts)
    end
  when 401
    AuthenticationError.new(error_data[:message], opts)
  when 404
    ResourceNotFoundError.new(error_data[:message], opts)
  when 429
    RateLimitExceededError.new(error_data[:message], opts)
  when 500
    InternalServerError.new(error_data[:message], opts)
  when 503
    ServiceUnavailableError.new(error_data[:message], opts)
  else
    APIError.new(error_data[:message], opts)
  end
end