class Pin::PinError

Base Pin Error class

Public Class Methods

handle_bad_request() click to toggle source
# File lib/pin_up/pin_errors.rb, line 60
def self.handle_bad_request
  raise Pin::ClientError, 'request :method must be one of get, post, put, patch or delete'
end
handle_bad_response(response) click to toggle source
# File lib/pin_up/pin_errors.rb, line 44
def self.handle_bad_response(response)
  message = ''
  begin
    response['messages'].each do |m|
      message += "#{m['code']}: #{m['message']} "
    end
  rescue
    begin response['messages']['amount'][0]
      message = "#{response['error']}: #{response['error_description']}. Amount: #{response['messages']['amount'][0]} "
    rescue
      message = "#{response['error']}: #{response['error_description']}"
    end
  end
  message
end
handle_error(http_status, response) click to toggle source
# File lib/pin_up/pin_errors.rb, line 15
def self.handle_error(http_status, response)
  case http_status
  when 400
    case response['error']
    when 'cannot_delete_primary_card'
      raise Pin::InvalidResource.new(response, response['error_description'])
    when 'invalid_card'
      raise Pin::InvalidResource.new(response, response['error_description'])
    when 'invalid_state'
      raise Pin::InvalidResource.new(response, response['error_description'])
    when 'invalid_request'
      raise Pin::InvalidResource.new(response, response['error_description'])
    else
      raise Pin::ChargeError.new(response)
    end
  when 401
    raise Pin::Unauthorized.new(response)
  when 402
    raise Pin::InsufficientPinBalance.new(response)
  when 404
    raise Pin::ResourceNotFound.new response
  when 422
    message = handle_bad_response(response)
    raise Pin::InvalidResource.new(response, message)
  else
    raise Pin::Unknown.new(response, "Unknown error with status code #{http_status}")
  end
end
new(message = nil, http_status = nil) click to toggle source
# File lib/pin_up/pin_errors.rb, line 5
def initialize(message = nil, http_status = nil)
  @message = message
  @http_status = http_status
end

Public Instance Methods

to_s() click to toggle source
# File lib/pin_up/pin_errors.rb, line 10
def to_s
  status_string = @http_status.nil? ? '' : "(Status #{@http_status}) "
  "#{status_string}#{@message}"
end