class Paysafe::Error

Constants

BadGateway

Raised on the HTTP status code 502

BadRequest

Raised on the HTTP status code 400

ClientError

Raised on a 4xx HTTP status code

Conflict

Raised on the HTTP status code 409

ERRORS_BY_STATUS
Forbidden

Raised on the HTTP status code 403

GatewayTimeout

Raised on the HTTP status code 504

InternalServerError

Raised on the HTTP status code 500

NotAcceptable

Raised on the HTTP status code 406

NotFound

Raised on the HTTP status code 404

RequestDeclined

Raised on the HTTP status code 402

ServerError

Raised on a 5xx HTTP status code

ServiceUnavailable

Raised on the HTTP status code 503

TooManyRequests

Raised on the HTTP status code 429

Unauthorized

Raised on the HTTP status code 401

UnprocessableEntity

Raised on the HTTP status code 422

UnsupportedMediaType

Raised on the HTTP status code 415

Attributes

code[R]

The Paysafe API Error Code

@return [String]

response[R]

The JSON HTTP response in Hash form

@return [Hash]

Public Class Methods

from_response(body, status) click to toggle source

Create a new error from an HTTP response

@param body [String] @param status [Integer] @return [Paysafe::Error]

# 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
new(message: '', code: nil, response: {}) click to toggle source

Initializes a new Error object

@param message [Exception, String] @param code [String] @param response [Hash] @return [Paysafe::Error]

Calls superclass method
# File lib/paysafe/error.rb, line 107
def initialize(message: '', code: nil, response: {})
  @code = code
  @response = response
  super(message)
end

Private Class Methods

parse_error(body) click to toggle source
# File lib/paysafe/error.rb, line 82
def parse_error(body)
  if body.is_a?(Hash)
    [ body.dig(:error, :message), body.dig(:error, :code) ]
  else
    [ '', nil ]
  end
end

Public Instance Methods

id() click to toggle source
# File lib/paysafe/error.rb, line 117
def id
  response.dig(:id) if response.is_a?(Hash)
end
merchant_ref_num() click to toggle source
# File lib/paysafe/error.rb, line 121
def merchant_ref_num
  response.dig(:merchant_ref_num) if response.is_a?(Hash)
end
to_s() click to toggle source
Calls superclass method
# File lib/paysafe/error.rb, line 113
def to_s
  [ super, code_text, field_error_text, detail_text ].compact.join(' ')
end

Private Instance Methods

code_text() click to toggle source
# File lib/paysafe/error.rb, line 127
def code_text
  "(Code #{code})" if code
end
detail_text() click to toggle source
# File lib/paysafe/error.rb, line 142
def detail_text
  "Details: #{details.join('. ')}".strip if details
end
details() click to toggle source
# File lib/paysafe/error.rb, line 146
def details
  response.dig(:error, :details) if response.is_a?(Hash)
end
field_error_text() click to toggle source
# File lib/paysafe/error.rb, line 131
def field_error_text
  if field_errors
    msgs = field_errors.map { |f| "The \`#{f[:field]}\` #{f[:error]}." }.join(' ')
    "Field Errors: #{msgs}".strip
  end
end
field_errors() click to toggle source
# File lib/paysafe/error.rb, line 138
def field_errors
  response.dig(:error, :field_errors) if response.is_a?(Hash)
end