Sha256: e7234bf2d90859882d78ec9348ddb7efb17deebec0d03306d7be35539601d777

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Recurly
  module Errors
    class APIError < StandardError
      # @!attribute recurly_error
      #   @return [Recurly::Resources::Error] The {Recurly::Resources::Error} object
      attr_reader :recurly_error

      # Looks up an Error class by name
      # @example
      #   Errors.error_class('BadRequestError')
      #   #=> Errors::BadRequestError
      # @param error_key [String]
      # @return [Errors::APIError,Errors::NetworkError]
      def self.error_class(error_key)
        class_name = error_key.split("_").map(&:capitalize).join
        class_name += "Error" unless class_name.end_with?("Error")
        Errors.const_get(class_name)
      end

      # When the response does not have a JSON body, this determines the appropriate
      # Error class based on the response code. This may occur when a load balancer
      # returns an error before it reaches Recurly's API.
      # @param response [Net::Response]
      # @return [Errors::APIError]
      def self.from_response(response)
        if Recurly::Errors::ERROR_MAP.has_key?(response.code)
          Recurly::Errors.const_get(Recurly::Errors::ERROR_MAP[response.code])
        else
          Recurly::Errors::APIError
        end
      end

      def initialize(message, response = nil, error = nil)
        super(message)
        @response = response
        @recurly_error = error
      end

      def status_code
        @response.status
      end

      def get_response
        @response
      end
    end
  end

  require_relative "./errors/api_errors"
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
recurly-4.0.1 lib/recurly/errors.rb
recurly-4.0.0 lib/recurly/errors.rb