module CoinbaseCommerce::Errors

Public Class Methods

general_api_error(status, body) click to toggle source
# File lib/coinbase_commerce/api_errors.rb, line 102
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) click to toggle source

Errors handling

# File lib/coinbase_commerce/api_errors.rb, line 88
def self.handle_error_response(http_resp)
  begin
    resp = CoinbaseCommerceResponse.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) click to toggle source
# File lib/coinbase_commerce/api_errors.rb, line 142
def self.handle_network_error(e, api_base = nil)
  api_base ||= @api_uri
  case e
  when Faraday::ConnectionFailed
    message = "Unexpected error communicating when trying to connect to Coinbase Commerce."
  when Faraday::SSLError
    message = "Could not establish a secure connection to Coinbase Commerce."
  when Faraday::TimeoutError
    message = "Could not connect to Coinbase Commerce (#{api_base})."
  else
    message = "Unexpected error communicating with Coinbase Commerce."
  end
  raise APIConnectionError, message + "\n\n(Network error: #{e.message})"
end
specific_api_error(resp, error_data) click to toggle source
# File lib/coinbase_commerce/api_errors.rb, line 107
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 then
    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