Exception: SynapsePayRest::Error

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

Overview

Custom class for handling HTTP and API errors.

Constant Summary collapse

ClientError =

Raised on a 4xx HTTP status code

Class.new(self)
BadRequest =

Raised on the HTTP status code 400

Class.new(ClientError)
Unauthorized =

Raised on the HTTP status code 401

Class.new(ClientError)
RequestDeclined =

Raised on the HTTP status code 402

Class.new(ClientError)
Forbidden =

Raised on the HTTP status code 403

Class.new(ClientError)
NotFound =

Raised on the HTTP status code 404

Class.new(ClientError)
NotAcceptable =

Raised on the HTTP status code 406

Class.new(ClientError)
Conflict =

Raised on the HTTP status code 409

Class.new(ClientError)
UnsupportedMediaType =

Raised on the HTTP status code 415

Class.new(ClientError)
UnprocessableEntity =

Raised on the HTTP status code 422

Class.new(ClientError)
TooManyRequests =

Raised on the HTTP status code 429

Class.new(ClientError)
ServerError =

Raised on a 5xx HTTP status code

Class.new(self)
InternalServerError =

Raised on the HTTP status code 500

Class.new(ServerError)
BadGateway =

Raised on the HTTP status code 502

Class.new(ServerError)
ServiceUnavailable =

Raised on the HTTP status code 503

Class.new(ServerError)
GatewayTimeout =

Raised on the HTTP status code 504

Class.new(ServerError)
ERRORS =
TODO:

need to add an error message for various 202 cases (fingerprint, mfa, etc)

TODO:

doesn’t do well when there’s an html response from nginx for bad gateway/timeout

HTTP status code to Error subclass mapping

{
  '400' => SynapsePayRest::Error::BadRequest,
  '401' => SynapsePayRest::Error::Unauthorized,
  '402' => SynapsePayRest::Error::RequestDeclined,
  '403' => SynapsePayRest::Error::Forbidden,
  '404' => SynapsePayRest::Error::NotFound,
  '406' => SynapsePayRest::Error::NotAcceptable,
  '409' => SynapsePayRest::Error::Conflict,
  '415' => SynapsePayRest::Error::UnsupportedMediaType,
  '422' => SynapsePayRest::Error::UnprocessableEntity,
  '429' => SynapsePayRest::Error::TooManyRequests,
  '500' => SynapsePayRest::Error::InternalServerError,
  '502' => SynapsePayRest::Error::BadGateway,
  '503' => SynapsePayRest::Error::ServiceUnavailable,
  '504' => SynapsePayRest::Error::GatewayTimeout
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: '', code: nil, response: {}) ⇒ SynapsePayRest::Error

Initializes a new Error object

Parameters:

  • message (Exception, String) (defaults to: '')
  • code (Integer) (defaults to: nil)
  • response (Hash) (defaults to: {})


115
116
117
118
119
# File 'lib/synapse_pay_rest/error.rb', line 115

def initialize(message: '', code: nil, response: {})
  super(message)
  @code     = code
  @response = response
end

Instance Attribute Details

#codeInteger (readonly)

The SynapsePay API Error Code

Returns:

  • (Integer)


76
77
78
# File 'lib/synapse_pay_rest/error.rb', line 76

def code
  @code
end

#responseHash (readonly)

The JSON HTTP response in Hash form

Returns:

  • (Hash)


81
82
83
# File 'lib/synapse_pay_rest/error.rb', line 81

def response
  @response
end

Class Method Details

.from_response(body) ⇒ SynapsePayRest::Error

Create a new error from an HTTP response

Parameters:

  • body (String)
  • code (Integer)

Returns:



89
90
91
92
93
94
# File 'lib/synapse_pay_rest/error.rb', line 89

def from_response(body)
  message, error_code, http_code = parse_error(body)
  http_code = http_code.to_s
  klass = ERRORS[http_code] || SynapsePayRest::Error
  klass.new(message: message, code: error_code, response: body)
end