Exception: Recurly::Errors::APIError

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

Direct Known Subclasses

ClientError, ResponseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, error) ⇒ APIError

rubocop:enable Metrics/CyclomaticComplexity



50
51
52
53
54
# File 'lib/recurly/errors.rb', line 50

def initialize(response, error)
  super(error.message)
  @response = response
  @recurly_error = error
end

Instance Attribute Details

#recurly_errorObject

Returns the value of attribute recurly_error.



6
7
8
# File 'lib/recurly/errors.rb', line 6

def recurly_error
  @recurly_error
end

Class Method Details

.error_class(error_key) ⇒ Errors::APIError, Errors::NetworkError

Looks up an Error class by name

Examples:

Errors.error_class('BadRequestError')
#=> Errors::BadRequestError

Parameters:

  • error_key (String)

Returns:



14
15
16
17
18
# File 'lib/recurly/errors.rb', line 14

def self.error_class(error_key)
  class_name = error_key.split("_").map(&:capitalize).join
  class_name += "Error" unless class_name.end_with?("Error")
  Errors.const_get(class_name)
end

.from_response(response) ⇒ Errors::APIError, Errors::NetworkError

When the response does not have a JSON body, this determines the appropriate Error class based on the response code. This may occur when a load balancer returns an error before it reaches Recurly's API. rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • response (Net::Response)

Returns:



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

def self.from_response(response)
  case response
  when Net::HTTPBadRequest # 400
    Recurly::Errors::BadRequestError
  when Net::HTTPUnauthorized, Net::HTTPForbidden # 401, 403
    Recurly::Errors::UnauthorizedError
  when Net::HTTPRequestTimeOut # 408
    Recurly::Errors::TimeoutError
  when Net::HTTPTooManyRequests # 429
    Recurly::Errors::RateLimitedError
  when Net::HTTPInternalServerError # 500
    Recurly::Errors::InternalServerError
  when Net::HTTPServiceUnavailable # 503
    Recurly::Errors::UnavailableError
  when Net::HTTPGatewayTimeOut # 504
    Recurly::Errors::TimeoutError
  when Net::HTTPServerError # 5xx
    Recurly::Errors::UnavailableError
  else
    Recurly::Errors::APIError
  end
end

Instance Method Details

#get_responseObject



60
61
62
# File 'lib/recurly/errors.rb', line 60

def get_response
  @response
end

#status_codeObject



56
57
58
# File 'lib/recurly/errors.rb', line 56

def status_code
  @response.status
end