class Redd::Error

An error from reddit TODO: Move Error to an Errors module in next minor version?

Constants

Archived

This item has been archived and can no longer be edited.

AuthenticationRequired
BadGateway

Bad Gateway. Either a network or a reddit error. Either way, try again.

BadRequest
Conflict
ExpiredCode

You already received an access token using this code. The user should grant you access again to get a new code.

InternalServerError

There is an issue on reddit's end. Try again.

InvalidCaptcha
InvalidClassName
InvalidCredentials

Either your username or your password is wrong.

InvalidGrantType
InvalidMultiredditName
InvalidOAuth2Credentials

Your client id or your secret is wrong.

InvalidRequest
InvalidResponseType
InvalidScope

You don't have the proper scope to perform this request.

JSONError

Looks like we didn't get a JSON response. Raise this error.

NoTokenGiven

No access token was given.

NotFound

Four, oh four! The thing you're looking for wasn't found.

OAuth2AccessDenied
PermissionDenied
RequestError
ServiceUnavailable

Issue on reddit's end. Try again.

TimedOut

The connection timed out. Try again.

TooManyClassNames

Attributes

body[R]
code[R]
headers[R]

Public Class Methods

from_response(env) click to toggle source
# File lib/redd/error.rb, line 15
def self.from_response(env) # rubocop:disable all
  status = env[:status]
  body = parse_error(env[:body]).to_s
  case status
  when 200
    case body
    when /access_denied/i             then OAuth2AccessDenied
    when /unsupported_response_type/i then InvalidResponseType
    when /unsupported_grant_type/i    then InvalidGrantType
    when /invalid_scope/i             then InvalidScope
    when /invalid_request/i           then InvalidRequest
    when /no_text/i                   then NoTokenGiven
    when /invalid_grant/i             then ExpiredCode
    when /wrong_password/i            then InvalidCredentials
    when /bad_captcha/i               then InvalidCaptcha
    when /ratelimit/i                 then RateLimited
    when /quota_filled/i              then QuotaFilled
    when /bad_css_name/i              then InvalidClassName
    when /too_old/i                   then Archived
    when /too_much_flair_css/i        then TooManyClassNames
    when /user_required/i             then AuthenticationRequired
    end
  when 400 then BadRequest
  when 401 then InvalidOAuth2Credentials
  when 403
    if /user_required/i =~ body
      AuthenticationRequired
    else
      PermissionDenied
    end
  when 404 then NotFound
  when 409 then Conflict
  when 500 then InternalServerError
  when 502 then BadGateway
  when 503 then ServiceUnavailable
  when 504 then TimedOut
  end
end
new(env) click to toggle source
# File lib/redd/error.rb, line 9
def initialize(env)
  @code = env[:status]
  @headers = env[:response_headers]
  @body = env[:body]
end
parse_error(body) click to toggle source
# File lib/redd/error.rb, line 54
def self.parse_error(body) # rubocop:disable all
  return body unless body.is_a?(Hash)

  if body.key?(:json) && body[:json].key?(:errors)
    body[:json][:errors].first
  elsif body.key?(:jquery)
    body[:jquery]
  elsif body.key?(:error)
    body[:error]
  elsif body.key?(:code) && body[:code] == 'NO_TEXT'
    'NO_TEXT'
  end
end