class Twitter::Error

Custom error class for rescuing from all Twitter errors

Constants

AlreadyFavorited

Raised when a Tweet has already been favorited

AlreadyPosted
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

Codes

If error code is missing see dev.twitter.com/overview/api/response-codes

ConfigurationError
DuplicateStatus

Raised when a Tweet has already been posted

EnhanceYourCalm
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

NotAcceptable

Raised when Twitter returns the HTTP status code 406

NotFound

Raised when Twitter returns the HTTP status code 404

RateLimited
RequestTimeout

Raised when Twitter returns the HTTP status code 408

ServerError

Raised when Twitter returns a 5xx HTTP status code

ServiceUnavailable

Raised when Twitter returns the HTTP status code 503

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

Attributes

code[R]

@return [Integer]

rate_limit[R]

@return [Twitter::RateLimit]

Public Class Methods

errors() click to toggle source

@return [Hash]

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

Create a new error from an HTTP response

@param response [Faraday::Response] @return [Twitter::Error]

# File lib/twitter/error.rb, line 45
def from_response(response)
  message, code = parse_error(response.body)
  new(message, response.response_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 105
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 89
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 79
def parse_error(body)
  if body.nil?
    ['', nil]
  elsif body[:error]
    [body[:error], nil]
  elsif body[:errors]
    extract_message_from_errors(body)
  end
end