Exception: Millionsend::Error

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

Overview

Base class for everything this SDK raises. Mirrors the API's wire error body { statusCode, name, message }; #status_code is nil for client-side and transport failures that never reached the API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status_code = nil, name = nil) ⇒ Error

Returns a new instance of Error.



10
11
12
13
14
# File 'lib/millionsend/error.rb', line 10

def initialize(message, status_code = nil, name = nil)
  super(message)
  @status_code = status_code
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/millionsend/error.rb', line 8

def name
  @name
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



8
9
10
# File 'lib/millionsend/error.rb', line 8

def status_code
  @status_code
end

Class Method Details

.from_response(status, body) ⇒ Object

Build the right subclass from a non-2xx response, keyed on the stable name discriminant. Falls back to ApplicationError for unknown names or a body that is not the canonical error shape.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/millionsend/error.rb', line 19

def self.from_response(status, body)
  if body.is_a?(Hash)
    name = body[:name].is_a?(String) ? body[:name] : "application_error"
    message = body[:message].is_a?(String) ? body[:message] : "Request failed with status #{status}"
    code = body[:statusCode].is_a?(Integer) ? body[:statusCode] : status
  else
    name = "application_error"
    message = "Request failed with status #{status}"
    code = status
  end
  (ERROR_TYPES[name] || ApplicationError).new(message, code, name)
end