Sha256: 3fa9f968d508024c050f74d24ee70ed5bf9ae0783e0df23ba0a666434292b1b1
Contents?: true
Size: 1.38 KB
Versions: 4
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true require_relative '../error' module Redd module Utilities # Handles response errors in API responses. class ErrorHandler HTTP_ERRORS = { 400 => Redd::BadRequest, # 403 => Redd::Forbidden, 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, raw:) # TODO: deal with errors of type { fields:, explanation:, message:, reason: } if !raw && response.body.is_a?(Hash) && response.body[:json] && response.body[:json][:errors] && !response.body[:json][:errors].empty? Redd::APIError.new(response) elsif HTTP_ERRORS.key?(response.code) HTTP_ERRORS[response.code].new(response) elsif response.code == 401 || response.code == 403 # FIXME: i think insufficient_scope comes with 403 and invalid_token with 401 AUTHORIZATION_ERRORS.each do |key, klass| auth_header = response.headers['www-authenticate'] return klass.new(response) if auth_header && auth_header.include?(key) end nil end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems