Exception: Twitter::Error

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

Overview

Custom error class for rescuing from all Twitter errors

Direct Known Subclasses

ClientError, ServerError

Defined Under Namespace

Modules: Code Classes: AlreadyFavorited, AlreadyRetweeted, BadGateway, BadRequest, ClientError, ConfigurationError, DuplicateStatus, Forbidden, GatewayTimeout, InternalServerError, NotAcceptable, NotFound, RequestTimeout, ServerError, ServiceUnavailable, TooManyRequests, Unauthorized, UnprocessableEntity

Constant Summary

Codes =

rubocop:disable ConstantName

Code
AlreadyPosted =

rubocop:disable ConstantName

DuplicateStatus
EnhanceYourCalm =

rubocop:disable ConstantName

TooManyRequests
RateLimited =

rubocop:disable ConstantName

TooManyRequests

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Initializes a new Error object

Parameters:

  • exception (Exception, String)
  • rate_limit (Hash) (defaults to: {})
  • code (Integer) (defaults to: nil)


97
98
99
100
101
# File 'lib/twitter/error.rb', line 97

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

Instance Attribute Details

- (Object) code (readonly)

Returns the value of attribute code



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

def code
  @code
end

- (Object) rate_limit (readonly)

Returns the value of attribute rate_limit



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

def rate_limit
  @rate_limit
end

Class Method Details

+ (Hash) errors

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/twitter/error.rb', line 44

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,
    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

+ (Object) forbidden_messages



61
62
63
64
65
66
67
# File 'lib/twitter/error.rb', line 61

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

+ (Twitter::Error) from_response(response)

Create a new error from an HTTP response

Parameters:

  • response (Faraday::Response)

Returns:



38
39
40
41
# File 'lib/twitter/error.rb', line 38

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