Sha256: 90f5757f14f29b738edb48a6e5a194729ebe1a06a0dba1b1033b5ce3000f84a6

Contents?: true

Size: 958 Bytes

Versions: 1

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true

require_relative '../error'

module Redd
  module Utilities
    # Handles response errors in API responses.
    # TODO: handle [:json][:errors] array
    class ErrorHandler
      HTTP_ERRORS = {
        400 => Redd::BadRequest,
        404 => Redd::NotFound,
        500 => Redd::ServerError,
        502 => Redd::ServerError,
        503 => Redd::ServerError,
        504 => Redd::ServerError
      }.freeze

      AUTHORIZATION_ERRORS = {
        'insufficient_scope' => Redd::InsufficientScope,
        'invalid_token' => Redd::InvalidAccess
      }.freeze

      def check_error(response)
        return HTTP_ERRORS[response.code].new(response) if HTTP_ERRORS.key?(response.code)
        if response.code == 403
          AUTHORIZATION_ERRORS.each do |key, klass|
            return klass.new(response) if response.headers['www-authenticate'].include?(key)
          end
        end
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redd-0.8.0.pre.1 lib/redd/utilities/error_handler.rb