Exception: IronBank::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/iron_bank/error.rb

Overview

Custom error class for rescuing from all Zuora API errors

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



33
34
35
36
37
38
39
# File 'lib/iron_bank/error.rb', line 33

def initialize(response = nil)
  @response = response

  message = response.is_a?(Faraday::Response) ? message_from_body : response

  super(message)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



31
32
33
# File 'lib/iron_bank/error.rb', line 31

def response
  @response
end

Class Method Details

.from_body(response) ⇒ Object



25
26
27
28
29
# File 'lib/iron_bank/error.rb', line 25

def self.from_body(response)
  return unless (match = CODE_MATCHER.match(response.body.to_s))

  CODE_CLASSES[match.captures.first]
end

.from_response(response) ⇒ Object

Returns the appropriate IronBank::Error subclass based on status and response message



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/iron_bank/error.rb', line 8

def self.from_response(response)
  klass = case response.status
          when 200      then from_body(response)
          when 400      then IronBank::BadRequestError
          when 401      then IronBank::UnauthorizedError
          when 404      then IronBank::NotFoundError
          when 422      then IronBank::UnprocessableEntityError
          when 429      then IronBank::TooManyRequestsError
          when 500      then IronBank::InternalServerError
          when 400..499 then IronBank::ClientError
          when 500..599 then IronBank::ServerError
          else IronBank::Error
          end

  klass&.new(response)
end

Instance Method Details

#message_from_bodyObject



41
42
43
# File 'lib/iron_bank/error.rb', line 41

def message_from_body
  response && "Body: #{response.body}"
end