Exception: Twitter::Error

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

Overview

Custom error class for rescuing from all Twitter errors

Defined Under Namespace

Modules: Code Classes: AlreadyFavorited, AlreadyRetweeted, BadGateway, BadRequest, DuplicateStatus, Forbidden, GatewayTimeout, InternalServerError, InvalidMedia, MediaInternalError, NotAcceptable, NotFound, RequestEntityTooLarge, ServiceUnavailable, TooManyRequests, Unauthorized, UnprocessableEntity, UnsupportedMedia

Constant Summary collapse

ClientError =

Raised when Twitter returns a 4xx HTTP status code

Class.new(self)
ServerError =

Raised when Twitter returns a 5xx HTTP status code

Class.new(self)
MediaError =

Raised when Twitter returns a media related error

Class.new(self)
TimeoutError =

Raised when an operation subject to timeout takes too long

Class.new(self)
ERRORS =

Maps HTTP status codes to error classes

{
  400 => Twitter::Error::BadRequest,
  401 => Twitter::Error::Unauthorized,
  403 => Twitter::Error::Forbidden,
  404 => Twitter::Error::NotFound,
  406 => Twitter::Error::NotAcceptable,
  413 => Twitter::Error::RequestEntityTooLarge,
  420 => Twitter::Error::TooManyRequests,
  422 => Twitter::Error::UnprocessableEntity,
  429 => Twitter::Error::TooManyRequests,
  500 => Twitter::Error::InternalServerError,
  502 => Twitter::Error::BadGateway,
  503 => Twitter::Error::ServiceUnavailable,
  504 => Twitter::Error::GatewayTimeout
}.freeze
FORBIDDEN_MESSAGES =

Maps forbidden message patterns to error classes

proc do |message|
  case message
  when /(?=.*status).*duplicate/i
    # - "Status is a duplicate."
    Twitter::Error::DuplicateStatus
  when /already favorited/i
    # - "You have already favorited this status."
    Twitter::Error::AlreadyFavorited
  when /already retweeted|Share validations failed/i
    # - "You have already retweeted this Tweet." (Nov 2017-)
    # - "You have already retweeted this tweet." (?-Nov 2017)
    # - "sharing is not permissible for this status (Share validations failed)" (-? 2017)
    Twitter::Error::AlreadyRetweeted
  end
end
MEDIA_ERRORS =

Maps media error names to error classes

{
  "InternalError" => Twitter::Error::MediaInternalError,
  "InvalidMedia" => Twitter::Error::InvalidMedia,
  "UnsupportedMedia" => Twitter::Error::UnsupportedMedia
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Constructor Details

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

Initializes a new Error object

Examples:

Twitter::Error.new("Something went wrong", {}, 123)

Parameters:

  • message (Exception, String) (defaults to: "")

    The error message

  • rate_limit (Hash) (defaults to: {})

    The rate limit headers

  • code (Integer) (defaults to: nil)

    The error code



272
273
274
275
276
# File 'lib/twitter/error.rb', line 272

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

Instance Attribute Details

#codeInteger (readonly)

The error code from Twitter

Examples:

error.code

Returns:

  • (Integer)


12
13
14
# File 'lib/twitter/error.rb', line 12

def code
  @code
end

#rate_limitTwitter::RateLimit (readonly)

The rate limit information from the response

Examples:

error.rate_limit

Returns:



20
21
22
# File 'lib/twitter/error.rb', line 20

def rate_limit
  @rate_limit
end

Class Method Details

.from_processing_response(error, headers) ⇒ Twitter::MediaError

Creates a new error from a media error hash

Examples:

Twitter::Error.from_processing_response(error, headers)

Parameters:

  • error (Hash)

    The error hash from the response

  • headers (Hash)

    The response headers

Returns:

  • (Twitter::MediaError)


224
225
226
227
228
229
# File 'lib/twitter/error.rb', line 224

def from_processing_response(error, headers)
  klass = MEDIA_ERRORS[error[:name]] || self
  message = error[:message]
  code = error[:code]
  klass.new(message, headers, code)
end

.from_response(body, headers) ⇒ Twitter::Error

Creates a new error from an HTTP response

Examples:

Twitter::Error.from_response(body, headers)

Parameters:

  • body (String)

    The response body

  • headers (Hash)

    The response headers

Returns:



211
212
213
214
# File 'lib/twitter/error.rb', line 211

def from_response(body, headers)
  message, code = parse_error(body)
  new(message, headers, code)
end