Exception: Twitter::Error

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

Overview

Custom error class for rescuing from all Twitter errors

Defined Under Namespace

Modules: Code Classes: UnacceptableIO

Constant Summary

Codes =

rubocop:disable ConstantName

Code
ConfigurationError =
Class.new(::ArgumentError)
ClientError =

Raised when Twitter returns a 4xx HTTP status code

Class.new(self)
BadRequest =

Raised when Twitter returns the HTTP status code 400

Class.new(ClientError)
Unauthorized =

Raised when Twitter returns the HTTP status code 401

Class.new(ClientError)
Forbidden =

Raised when Twitter returns the HTTP status code 403

Class.new(ClientError)
AlreadyFavorited =

Raised when a Tweet has already been favorited

Class.new(Forbidden)
AlreadyRetweeted =

Raised when a Tweet has already been retweeted

Class.new(Forbidden)
DuplicateStatus =

Raised when a Tweet has already been posted

Class.new(Forbidden)
AlreadyPosted =

rubocop:disable ConstantName

DuplicateStatus
NotFound =

Raised when Twitter returns the HTTP status code 404

Class.new(ClientError)
NotAcceptable =

Raised when Twitter returns the HTTP status code 406

Class.new(ClientError)
RequestTimeout =

Raised when Twitter returns the HTTP status code 408

Class.new(ClientError)
UnprocessableEntity =

Raised when Twitter returns the HTTP status code 422

Class.new(ClientError)
TooManyRequests =

Raised when Twitter returns the HTTP status code 429

Class.new(ClientError)
EnhanceYourCalm =

rubocop:disable ConstantName

TooManyRequests
RateLimited =

rubocop:disable ConstantName

TooManyRequests
ServerError =

Raised when Twitter returns a 5xx HTTP status code

Class.new(self)
InternalServerError =

Raised when Twitter returns the HTTP status code 500

Class.new(ServerError)
BadGateway =

Raised when Twitter returns the HTTP status code 502

Class.new(ServerError)
ServiceUnavailable =

Raised when Twitter returns the HTTP status code 503

Class.new(ServerError)
GatewayTimeout =

Raised when Twitter returns the HTTP status code 504

Class.new(ServerError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '', rate_limit = {}, code = nil) ⇒ Twitter::Error

Initializes a new Error object



101
102
103
104
105
# File 'lib/twitter/error.rb', line 101

def initialize(message = '', rate_limit = {}, code = nil)
  super(message)
  @rate_limit = Twitter::RateLimit.new(rate_limit)
  @code = code
end

Instance Attribute Details

#codeInteger (readonly)



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

def code
  @code
end

#rate_limitTwitter::RateLimit (readonly)



9
10
11
# File 'lib/twitter/error.rb', line 9

def rate_limit
  @rate_limit
end

Class Method Details

.errorsHash



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/twitter/error.rb', line 47

def errors
  @errors ||= {
    400 => Twitter::Error::BadRequest,
    401 => Twitter::Error::Unauthorized,
    403 => Twitter::Error::Forbidden,
    404 => Twitter::Error::NotFound,
    406 => Twitter::Error::NotAcceptable,
    408 => Twitter::Error::RequestTimeout,
    420 => Twitter::Error::EnhanceYourCalm,
    422 => Twitter::Error::UnprocessableEntity,
    429 => Twitter::Error::TooManyRequests,
    500 => Twitter::Error::InternalServerError,
    502 => Twitter::Error::BadGateway,
    503 => Twitter::Error::ServiceUnavailable,
    504 => Twitter::Error::GatewayTimeout,
  }
end

.forbidden_messagesObject



65
66
67
68
69
70
71
# File 'lib/twitter/error.rb', line 65

def forbidden_messages
  @forbidden_messages ||= {
    'Status is a duplicate.' => Twitter::Error::DuplicateStatus,
    'You have already favorited this status.' => Twitter::Error::AlreadyFavorited,
    'sharing is not permissible for this status (Share validations failed)' => Twitter::Error::AlreadyRetweeted,
  }
end

.from_response(response) ⇒ Twitter::Error

Create a new error from an HTTP response



41
42
43
44
# File 'lib/twitter/error.rb', line 41

def from_response(response)
  message, code = parse_error(response.body)
  new(message, response.response_headers, code)
end