Class: Pin::PinError

Inherits:
Object
  • Object
show all
Defined in:
lib/pin_up/pin_errors.rb

Overview

Base Pin Error class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, http_status = nil) ⇒ PinError

Returns a new instance of PinError.



5
6
7
8
# File 'lib/pin_up/pin_errors.rb', line 5

def initialize(message = nil, http_status = nil)
  @message = message
  @http_status = http_status
end

Class Method Details

.handle_bad_requestObject

Raises:



60
61
62
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Method Details

#to_sObject



10
11
12
13
# File 'lib/pin_up/pin_errors.rb', line 10

def to_s
  status_string = @http_status.nil? ? '' : "(Status #{@http_status}) "
  "#{status_string}#{@message}"
end