Sha256: 56629a8f52ed2bbbd945c307d9beeecd4e770d739673966326e2584755880489

Contents?: true

Size: 792 Bytes

Versions: 6

Compression:

Stored size: 792 Bytes

Contents

require 'twitter/error'

module Twitter
  class Error
    # Raised when Twitter returns a 4xx HTTP status code or there's an error in Faraday
    class ClientError < Twitter::Error

      # Create a new error from an HTTP environment
      #
      # @param response [Hash]
      # @return [Twitter::Error]
      def self.from_response(response={})
        new(parse_error(response[:body]), response[:response_headers])
      end

    private

      def self.parse_error(body)
        if body.nil?
          ''
        elsif body[:error]
          body[:error]
        elsif body[:errors]
          first = Array(body[:errors]).first
          if first.is_a?(Hash)
            first[:message].chomp
          else
            first.chomp
          end
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
twitter-4.8.1 lib/twitter/error/client_error.rb
twitter-4.8.0 lib/twitter/error/client_error.rb
twitter-4.7.0 lib/twitter/error/client_error.rb
twitter-4.6.2 lib/twitter/error/client_error.rb
twitter-4.6.1 lib/twitter/error/client_error.rb
twitter-4.6.0 lib/twitter/error/client_error.rb