Exception: Redd::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/redd/error.rb

Overview

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

Direct Known Subclasses

RateLimited

Defined Under Namespace

Classes: QuotaFilled, RateLimited

Constant Summary collapse

Archived =

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

Class.new(Error)
AuthenticationRequired =
Class.new(Error)
BadGateway =

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

Class.new(Error)
BadRequest =
Class.new(Error)
Conflict =
Class.new(Error)
ExpiredCode =

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

Class.new(Error)
InternalServerError =

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

Class.new(Error)
InvalidCaptcha =
Class.new(Error)
InvalidClassName =
Class.new(Error)
InvalidCredentials =

Either your username or your password is wrong.

Class.new(Error)
InvalidGrantType =
Class.new(Error)
InvalidMultiredditName =
Class.new(Error)
InvalidOAuth2Credentials =

Your client id or your secret is wrong.

Class.new(Error)
InvalidResponseType =
Class.new(Error)
InvalidRequest =
Class.new(Error)
InvalidScope =

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

Class.new(Error)
JSONError =

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

Class.new(Error)
NotFound =

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

Class.new(Error)
NoTokenGiven =

No access token was given.

Class.new(Error)
OAuth2AccessDenied =
Class.new(Error)
PermissionDenied =
Class.new(Error)
RequestError =
Class.new(Error)
ServiceUnavailable =

Issue on reddit’s end. Try again.

Class.new(Error)
TimedOut =

The connection timed out. Try again.

Class.new(Error)
TooManyClassNames =
Class.new(Error)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Error

Returns a new instance of Error.



9
10
11
12
13
# File 'lib/redd/error.rb', line 9

def initialize(env)
  @code = env[:status]
  @headers = env[:response_headers]
  @body = env[:body]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/redd/error.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/redd/error.rb', line 5

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/redd/error.rb', line 6

def headers
  @headers
end

Class Method Details

.from_response(env) ⇒ Object

rubocop:disable all



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

.parse_error(body) ⇒ Object

rubocop:disable all



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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