Module: CoinbaseCommerce::Errors

Defined in:
lib/coinbase_commerce/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



102
103
104
105
# 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) ⇒ Object

Errors handling



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

Raises:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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) ⇒ Object



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
134
135
136
137
138
139
140
# 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