Sha256: ed1166b8842a37e39f2b3a179c0af198eb293f3ed4e08141aae3e9baa89b0b8b

Contents?: true

Size: 952 Bytes

Versions: 2

Compression:

Stored size: 952 Bytes

Contents

require 'faraday'
require 'twitter/error'

module Twitter
  module REST
    module Response
      class RaiseError < Faraday::Response::Middleware
        def on_complete(response)
          status_code = response.status.to_i
          klass = Twitter::Error.errors[status_code]
          return unless klass
          error = if klass == Twitter::Error::Forbidden
            handle_forbidden_errors(response)
          else
            klass.from_response(response)
          end
          fail(error)
        end

      private

        def handle_forbidden_errors(response)
          error = Twitter::Error::Forbidden.from_response(response)
          klass = Twitter::Error.forbidden_messages[error.message]
          if klass
            klass.from_response(response)
          else
            error
          end
        end
      end
    end
  end
end

Faraday::Response.register_middleware :raise_error => Twitter::REST::Response::RaiseError

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-5.11.0 lib/twitter/rest/response/raise_error.rb
twitter-5.10.0 lib/twitter/rest/response/raise_error.rb