Sha256: 2ba00e27699d2ca658ce8d59fcd66a8ac1a49a8218b41bb3640607b6056f66b8

Contents?: true

Size: 982 Bytes

Versions: 8

Compression:

Stored size: 982 Bytes

Contents

require 'twitter/rate_limit'

module Twitter
  # Custom error class for rescuing from all Twitter errors
  class Error < StandardError
    attr_reader :rate_limit, :wrapped_exception

    # @return [Hash]
    def self.errors
      @errors ||= Hash[descendants.map{|klass| [klass.const_get(:HTTP_STATUS_CODE), klass]}]
    end

    # @return [Array]
    def self.descendants
      ObjectSpace.each_object(::Class).select{|klass| klass < self}
    end

    # Initializes a new Error object
    #
    # @param exception [Exception, String]
    # @param response_headers [Hash]
    # @return [Twitter::Error]
    def initialize(exception=$!, response_headers={})
      @rate_limit = Twitter::RateLimit.new(response_headers)
      @wrapped_exception = exception
      exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
    end

    def backtrace
      @wrapped_exception.respond_to?(:backtrace) ? @wrapped_exception.backtrace : super
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
twitter-4.8.1 lib/twitter/error.rb
twitter-4.4.0 lib/twitter/error.rb
twitter-4.3.0 lib/twitter/error.rb
twitter-4.2.0 lib/twitter/error.rb
twitter-4.1.2 lib/twitter/error.rb
twitter-4.1.1 lib/twitter/error.rb
twitter-4.1.0 lib/twitter/error.rb
twitter-4.0.0 lib/twitter/error.rb