Sha256: 8007d41ad5c81f9c8be97e6eebf3c5c9938d5b95e05641b3cd939b3b7550f5eb

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

require 'faraday'
require 'balanced/error'

# @api private
module Faraday

  class Response::RaiseBalancedError < Response::Middleware

    HTTP_STATUS_CODES = {
        300 => Balanced::MoreInformationRequired,
        400 => Balanced::BadRequest,
        401 => Balanced::Unauthorized,
        402 => Balanced::PaymentRequired,
        403 => Balanced::Forbidden,
        404 => Balanced::NotFound,
        405 => Balanced::MethodNotAllowed,
        409 => Balanced::Conflict,
        410 => Balanced::Gone,
        500 => Balanced::InternalServerError,
        501 => Balanced::NotImplemented,
        502 => Balanced::BadGateway,
        503 => Balanced::ServiceUnavailable,
        504 => Balanced::GatewayTimeout,
    }
    CATEGORY_CODE_MAP = {
        'bank-account-authentication-not-pending' =>
        Balanced::BankAccountVerificationFailure,
        'bank-account-authentication-failed' =>
        Balanced::BankAccountVerificationFailure,
        'bank-account-authentication-already-exists' =>
        Balanced::BankAccountVerificationFailure,
    }
    def on_complete(response)
      status_code = response[:status].to_i
      if response.key? :body and response[:body] != nil and response[:body]['errors']
        category_code = response[:body]['errors'][0]['category_code']
      else
        category_code = nil
      end
      error_class = CATEGORY_CODE_MAP[category_code] || HTTP_STATUS_CODES[status_code]
      raise error_class.new(response) if error_class
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
balanced-1.1.1 lib/balanced/response/balanced_exception_middleware.rb
balanced-1.1.0 lib/balanced/response/balanced_exception_middleware.rb
balanced-1.0.2 lib/balanced/response/balanced_exception_middleware.rb
balanced-1.0.1 lib/balanced/response/balanced_exception_middleware.rb
balanced-1.0 lib/balanced/response/balanced_exception_middleware.rb
balanced-1.0.beta1 lib/balanced/response/balanced_exception_middleware.rb