Exception: RedditKit::Error

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

Class Method Summary collapse

Class Method Details

.from_status_code_and_body(status_code, body) ⇒ Object



7
8
9
10
11
12
13
14
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
# File 'lib/redditkit/error.rb', line 7

def from_status_code_and_body(status_code, body)
  error_value = extract_error_value body
  return if status_code == 200 && error_value.nil?

  case status_code
  when 200
    if error_value =~ /WRONG_PASSWORD/i
      RedditKit::InvalidCredentials
    elsif error_value =~ /BAD_CAPTCHA/i
      RedditKit::InvalidCaptcha
    elsif error_value =~ /RATELIMIT/i
      RedditKit::RateLimited
    elsif error_value =~ /BAD_CSS_NAME/i
      RedditKit::InvalidClassName
    elsif error_value =~ /TOO_OLD/i
      RedditKit::Archived
    elsif error_value =~ /TOO_MUCH_FLAIR_CSS/i
      RedditKit::TooManyClassNames
    elsif error_value =~ /USER_REQUIRED/i
      RedditKit::AuthenticationRequired
    end
  when 400
    if error_value =~ /BAD_MULTI_NAME/i
      RedditKit::InvalidMultiredditName
    end
  when 403
    if error_value =~ /USER_REQUIRED/i
      RedditKit::AuthenticationRequired
    else
      RedditKit::PermissionDenied
    end
  when 409
    RedditKit::Conflict
  when 500
    RedditKit::InternalServerError
  when 502
    RedditKit::BadGateway
  when 503
    RedditKit::ServiceUnavailable
  when 504
    RedditKit::TimedOut
  end
end