class Twitter::Error

Custom error class for rescuing from all Twitter errors

Constants

AlreadyFavorited

Raised when a Tweet has already been favorited

AlreadyRetweeted

Raised when a Tweet has already been retweeted

BadGateway

Raised when Twitter returns the HTTP status code 502

BadRequest

Raised when Twitter returns the HTTP status code 400

ClientError

Raised when Twitter returns a 4xx HTTP status code

DuplicateStatus

Raised when a Tweet has already been posted

ERRORS
FORBIDDEN_MESSAGES
Forbidden

Raised when Twitter returns the HTTP status code 403

GatewayTimeout

Raised when Twitter returns the HTTP status code 504

InternalServerError

Raised when Twitter returns the HTTP status code 500

InvalidMedia

Raised when Twitter returns an InvalidMedia error

MEDIA_ERRORS
MediaError

Raised when Twitter returns a media related error

MediaInternalError

Raised when Twitter returns a media InternalError error

NotAcceptable

Raised when Twitter returns the HTTP status code 406

NotFound

Raised when Twitter returns the HTTP status code 404

RequestEntityTooLarge

Raised when Twitter returns the HTTP status code 413

ServerError

Raised when Twitter returns a 5xx HTTP status code

ServiceUnavailable

Raised when Twitter returns the HTTP status code 503

TimeoutError

Raised when an operation subject to timeout takes too long

TooManyRequests

Raised when Twitter returns the HTTP status code 429

Unauthorized

Raised when Twitter returns the HTTP status code 401

UnprocessableEntity

Raised when Twitter returns the HTTP status code 422

UnsupportedMedia

Raised when Twitter returns an UnsupportedMedia error

Attributes

code[R]

@return [Integer]

rate_limit[R]

@return [Twitter::RateLimit]

Public Class Methods

from_processing_response(error, headers) click to toggle source

Create a new error from a media error hash

@param error [Hash] @param headers [Hash] @return [Twitter::MediaError]

# File lib/twitter/error.rb, line 160
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) click to toggle source

Create a new error from an HTTP response

@param body [String] @param headers [Hash] @return [Twitter::Error]

# File lib/twitter/error.rb, line 150
def from_response(body, headers)
  message, code = parse_error(body)
  new(message, headers, code)
end
new(message = '', rate_limit = {}, code = nil) click to toggle source

Initializes a new Error object

@param message [Exception, String] @param rate_limit [Hash] @param code [Integer] @return [Twitter::Error]

Calls superclass method
# File lib/twitter/error.rb, line 195
def initialize(message = '', rate_limit = {}, code = nil)
  super(message)
  @rate_limit = Twitter::RateLimit.new(rate_limit)
  @code = code
end

Private Class Methods

extract_message_from_errors(body) click to toggle source
# File lib/twitter/error.rb, line 179
def extract_message_from_errors(body)
  first = Array(body[:errors]).first
  if first.is_a?(Hash)
    [first[:message].chomp, first[:code]]
  else
    [first.chomp, nil]
  end
end
parse_error(body) click to toggle source
# File lib/twitter/error.rb, line 169
def parse_error(body)
  if body.nil? || body.empty?
    ['', nil]
  elsif body[:error]
    [body[:error], nil]
  elsif body[:errors]
    extract_message_from_errors(body)
  end
end