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 => Errors::BadRequest,
  403 => Errors::Forbidden,
  404 => Errors::NotFound,
  429 => Errors::TooManyRequests,
  500 => Errors::ServerError,
  502 => Errors::ServerError,
  503 => Errors::ServerError,
  504 => Errors::ServerError
}

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: }
  rate_limit_error(res) || other_api_error(res)
end