Exception: Paysafe::Error

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

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_BY_STATUS =
{
  400 => Paysafe::Error::BadRequest,
  401 => Paysafe::Error::Unauthorized,
  402 => Paysafe::Error::RequestDeclined,
  403 => Paysafe::Error::Forbidden,
  404 => Paysafe::Error::NotFound,
  406 => Paysafe::Error::NotAcceptable,
  409 => Paysafe::Error::Conflict,
  415 => Paysafe::Error::UnsupportedMediaType,
  422 => Paysafe::Error::UnprocessableEntity,
  429 => Paysafe::Error::TooManyRequests,
  500 => Paysafe::Error::InternalServerError,
  502 => Paysafe::Error::BadGateway,
  503 => Paysafe::Error::ServiceUnavailable,
  504 => Paysafe::Error::GatewayTimeout,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a new Error object

Parameters:

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


107
108
109
110
111
# File 'lib/paysafe/error.rb', line 107

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

Instance Attribute Details

#codeString (readonly)

The Paysafe API Error Code

Returns:

  • (String)


94
95
96
# File 'lib/paysafe/error.rb', line 94

def code
  @code
end

#responseHash (readonly)

The JSON HTTP response in Hash form

Returns:

  • (Hash)


99
100
101
# File 'lib/paysafe/error.rb', line 99

def response
  @response
end

Class Method Details

.from_response(body, status) ⇒ Paysafe::Error

Create a new error from an HTTP response

Parameters:

  • body (String)
  • status (Integer)

Returns:



74
75
76
77
78
# File 'lib/paysafe/error.rb', line 74

def from_response(body, status)
  klass = ERRORS_BY_STATUS[status] || Paysafe::Error
  message, error_code = parse_error(body)
  klass.new(message: message, code: error_code, response: body)
end

Instance Method Details

#idObject



117
118
119
# File 'lib/paysafe/error.rb', line 117

def id
  response.dig(:id) if response.is_a?(Hash)
end

#merchant_ref_numObject



121
122
123
# File 'lib/paysafe/error.rb', line 121

def merchant_ref_num
  response.dig(:merchant_ref_num) if response.is_a?(Hash)
end

#to_sObject



113
114
115
# File 'lib/paysafe/error.rb', line 113

def to_s
  [ super, code_text, field_error_text, detail_text ].compact.join(' ')
end