class Redd::Utilities::ErrorHandler
Handles response errors in API responses.
Constants
- AUTH_HEADER
- HTTP_ERRORS
- INSUFFICIENT_SCOPE
- INVALID_TOKEN
Public Instance Methods
check_error(res, raw:)
click to toggle source
# 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
Private Instance Methods
api_error(res)
click to toggle source
Deal with those annoying errors that come with perfect 200 status codes.
# File lib/redd/utilities/error_handler.rb, line 57 def api_error(res) return nil unless res.body.is_a?(Hash) && res.body[:json] && res.body[:json][:errors] && !res.body[:json][:errors].empty? APIError.new(res) end
insufficient_scope_error(res)
click to toggle source
Deal with an error caused by not having enough the correct scope
# File lib/redd/utilities/error_handler.rb, line 45 def insufficient_scope_error(res) return nil unless res.code == 403 && res.headers[AUTH_HEADER] && res.headers[AUTH_HEADER].include?(INSUFFICIENT_SCOPE) InsufficientScope.new(res) end
invalid_access_error(res)
click to toggle source
Deal with an error caused by having an expired or invalid access token.
# File lib/redd/utilities/error_handler.rb, line 38 def invalid_access_error(res) return nil unless res.code == 401 && res.headers[AUTH_HEADER] && res.headers[AUTH_HEADER].include?(INVALID_TOKEN) InvalidAccess.new(res) end
other_http_error(res)
click to toggle source
Deal with an error signalled by the HTTP response code.
# File lib/redd/utilities/error_handler.rb, line 52 def other_http_error(res) HTTP_ERRORS[res.code].new(res) if HTTP_ERRORS.key?(res.code) end