Sha256: 1a70f6ffe38ea86b9da1619ae1b4794511f16d9efb45491b91af7abdaa806b53

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require_relative 'error/formatter'
require_relative 'error_handler/conflicting_field'
require_relative 'error_handler/default'
require_relative 'error_handler/expired_access_token'
require_relative 'error_handler/expired_code_grant'
require_relative 'error_handler/invalid_credentials'
require_relative 'error_handler/resource_not_found'
require_relative 'error_handler/unauthorized'

module RDStation
  class ErrorHandler
    ERROR_TYPES = [
      ErrorHandler::ConflictingField,
      ErrorHandler::ExpiredAccessToken,
      ErrorHandler::ExpiredCodeGrant,
      ErrorHandler::InvalidCredentials,
      ErrorHandler::ResourceNotFound,
      ErrorHandler::Unauthorized,
      ErrorHandler::Default
    ].freeze

    def initialize(response)
      @response = response
    end

    def raise_errors
      error_types.each(&:raise_error)
    end

    private

    attr_reader :response

    def array_of_errors
      error_formatter.to_array.map do |error|
        error.merge(additional_error_attributes)
      end
    end

    def error_types
      ERROR_TYPES.map { |error_type| error_type.new(array_of_errors) }
    end

    def response_errors
      JSON.parse(response.body)['errors']
    end

    def error_formatter
      @error_formatter = RDStation::Error::Formatter.new(response_errors)
    end

    def additional_error_attributes
      {
        'headers' => response.headers,
        'body' => JSON.parse(response.body),
        'http_status' => response.code
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdstation-ruby-client-1.0.1 lib/rdstation/error_handler.rb