Sha256: 68ba9f44e0f913741f1fc12e1fd2702d3578d5eae40537aca7ae969c232b2c2c

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Pin
  class PinError < Exception

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

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

    def self.handle_error(http_status, response)
      case http_status
      when 400
        raise(Pin::ChargeError, "#{response['error']}: #{response['error_description']}. Charge token: #{response['charge_token']}")
      when 402
        raise(Pin::InsufficientPinBalance, "#{response['error_description']}")
      when 404
        raise(Pin::ResourceNotFound, "#{response['error_description']}")
      when 422
        message = ""
          begin
            response['messages'].each do |m|
              message += "#{m['code']}: #{m['message']}"
            end             
           rescue
             message = "#{response['error']}: #{response['error_description']}. Amount: #{response['messages']['amount'][0]}"
           end 
        raise(Pin::InvalidResource, message)
      end
    end
  end

  class ResourceNotFound < PinError
  end

  class InvalidResource < PinError
  end

  class ChargeError < PinError
  end

  class InsufficientPinBalance <PinError
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pin_up-0.8.0 lib/pin_up/pin_errors.rb