Class: Redd::Utilities::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/utilities/error_handler.rb

Overview

Handles response errors in API responses.

Constant Summary collapse

AUTH_HEADER =
'www-authenticate'
INVALID_TOKEN =
'invalid_token'
INSUFFICIENT_SCOPE =
'insufficient_scope'
HTTP_ERRORS =
{
  400 => Redd::BadRequest,
  403 => Redd::Forbidden,
  404 => Redd::NotFound,
  429 => Redd::TooManyRequests,
  500 => Redd::ServerError,
  502 => Redd::ServerError,
  503 => Redd::ServerError,
  504 => Redd::ServerError
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_error(res, raw:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/redd/utilities/error_handler.rb', line 24

def check_error(res, raw:)
  # Check for status code-based errors first and return it if we found one.
  error = invalid_access_error(res) || insufficient_scope_error(res) || other_http_error(res)
  return error if error || raw

  # If there wasn't an status code error and we're allowed to look into the response, parse
  # it and check for errors.
  # TODO: deal with errors of type { fields:, explanation:, message:, reason: }
  api_error(res)
end