Sha256: bc5b97860b68efb6c41a1b06b086c9fbb92ca692e38faec13e89cc1be8a3dbdb

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'excon'
require 'peddler/errors/builder'

module Peddler
  module Errors
    # @api private
    class Handler
      DIGIT_AS_FIRST_CHAR = /^\d/
      private_constant :DIGIT_AS_FIRST_CHAR

      def self.call(exception)
        new(exception).handle
      end

      def self.parameters
        1
      end

      attr_reader :exception

      def initialize(exception)
        @exception = exception
      end

      def handle
        raise exception unless http_status_error?
        raise exception if bad_name_for_error_class?

        raise error_class.new(error_message, exception)
      end

      private

      def error_class
        Errors.const_get(error_name)
      rescue NameError
        Builder.build(error_name)
      end

      def http_status_error?
        exception.is_a?(::Excon::Error::HTTPStatus)
      end

      def bad_name_for_error_class?
        error_name =~ DIGIT_AS_FIRST_CHAR
      end

      def error_name
        exception.response.code
      end

      def error_message
        exception.response.message
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peddler-1.6.3 lib/peddler/errors/handler.rb
peddler-1.6.2 lib/peddler/errors/handler.rb
peddler-1.6.1 lib/peddler/errors/handler.rb