Skip to content

Commit

Permalink
Revert "Convert Twitter::Error class to module"
Browse files Browse the repository at this point in the history
This reverts commit 115aea1.
  • Loading branch information
sferik committed Jun 21, 2012
1 parent f24f221 commit 05d055d
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 215 deletions.
28 changes: 7 additions & 21 deletions README.md
Expand Up @@ -87,23 +87,23 @@ This functionality may be replicated by inserting custom Faraday middleware.

### Configuration

The Faraday middleware stack is now fully configurable and is exposed as a
The Faraday middleware stack is now fully configurable and is exposed as a
`Faraday::Builder` which can be manipulated in place:

Twitter.middleware.insert_after Twitter::Response::RaiseClientError, CustomMiddleware

Likewise, the middleware stack can be replaced in it's entirety:

Twitter.middleware = Faraday::Builder.new(&Proc.new { |builder|
# Specify a middleware stack here
})

As part of these configuration changes we've removed `adapter` configuration.
As part of these configuration changes we've removed `adapter` configuration.
If you would like to change the adapter used, you can do so by setting Faraday's
`default_adapter`:

Faraday.default_adapter = :some_other_adapter

The adapter can also be configured as part of the middleware stack:

Twitter.middleware = Faraday::Builder.new(&Proc.new { |builder|
Expand Down Expand Up @@ -133,23 +133,9 @@ false.)

### Errors

All HTTP errors are now tagged with the `Twitter::Error` module. There's a
distinct possibility you've written application code like this to prevent an
HTTP error from crashing your entire application:

begin
# Make Twitter requests
rescue Faraday::Error::ClientError, EOFError, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EINVAL, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError => error
log.error(error.message)
end

This code can now be replaced with:

begin
# Make Twitter requests
rescue Twitter::Error
log.error(error.message)
end
Any Faraday client errors are captured and re-raised as a
`Twitter::Error::ClientError`, so there's no longer a need to separately rescue
`Faraday::Error::ClientError`.

The `Twitter::Error::EnhanceYourCalm` class has been removed, since all Search
API requests are made via api.twitter.com, which does not return HTTP 420. When
Expand Down
48 changes: 24 additions & 24 deletions lib/twitter/error.rb
@@ -1,37 +1,37 @@
module Twitter
module Error
attr_reader :http_headers

module ClassMethods

def errors
return @errors if defined? @errors
array = descendants.map do |klass|
[klass.const_get(:HTTP_STATUS_CODE), klass]
end.flatten
@errors = Hash[*array]
end

private

def descendants
ObjectSpace.each_object(::Class).select{|klass| klass < self}
end
# Custom error class for rescuing from all Twitter errors
class Error < StandardError
attr_reader :http_headers, :wrapped_exception

def self.errors
return @errors if defined? @errors
array = descendants.map do |klass|
[klass.const_get(:HTTP_STATUS_CODE), klass]
end.flatten
@errors = Hash[*array]
end

def self.included(base)
base.extend(ClassMethods)
def self.descendants
ObjectSpace.each_object(::Class).select{|klass| klass < self}
end

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

def backtrace
@wrapped_exception ? @wrapped_exception.backtrace : super
end

# @return [Time]
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/bad_gateway.rb
@@ -1,7 +1,7 @@
require 'twitter/error/server_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 502
class BadGateway < Twitter::Error::ServerError
HTTP_STATUS_CODE = 502
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/bad_request.rb
@@ -1,7 +1,7 @@
require 'twitter/error/client_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 400
class BadRequest < Twitter::Error::ClientError
HTTP_STATUS_CODE = 400
Expand Down
9 changes: 4 additions & 5 deletions lib/twitter/error/client_error.rb
@@ -1,17 +1,16 @@
require 'twitter/error'

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

# Create a new error from a HTTP environment
# Create a new error from an HTTP environment
#
# @param env [Hash]
# @return [Twitter::Error]
def self.from_env(env)
new(error_body(env[:message]), env[:response_headers])
new(error_body(env[:body]), env[:response_headers])
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/forbidden.rb
@@ -1,7 +1,7 @@
require 'twitter/error/client_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 403
class Forbidden < Twitter::Error::ClientError
HTTP_STATUS_CODE = 403
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/internal_server_error.rb
@@ -1,7 +1,7 @@
require 'twitter/error/server_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 500
class InternalServerError < Twitter::Error::ServerError
HTTP_STATUS_CODE = 500
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/not_acceptable.rb
@@ -1,7 +1,7 @@
require 'twitter/error/client_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 406
class NotAcceptable < Twitter::Error::ClientError
HTTP_STATUS_CODE = 406
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/not_found.rb
@@ -1,7 +1,7 @@
require 'twitter/error/client_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 404
class NotFound < Twitter::Error::ClientError
HTTP_STATUS_CODE = 404
Expand Down
7 changes: 3 additions & 4 deletions lib/twitter/error/server_error.rb
@@ -1,13 +1,12 @@
require 'twitter/error'

module Twitter
module Error
class Error
# Raised when Twitter returns a 5xx HTTP status code
class ServerError < StandardError
include Twitter::Error
class ServerError < Twitter::Error
MESSAGE = "Server Error"

# Create a new error from a HTTP environment
# Create a new error from an HTTP environment
#
# @param env [Hash]
# @return [Twitter::Error]
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/service_unavailable.rb
@@ -1,7 +1,7 @@
require 'twitter/error/server_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 503
class ServiceUnavailable < Twitter::Error::ServerError
HTTP_STATUS_CODE = 503
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/error/unauthorized.rb
@@ -1,7 +1,7 @@
require 'twitter/error/client_error'

module Twitter
module Error
class Error
# Raised when Twitter returns the HTTP status code 401
class Unauthorized < Twitter::Error::ClientError
HTTP_STATUS_CODE = 401
Expand Down
5 changes: 2 additions & 3 deletions lib/twitter/requestable.rb
Expand Up @@ -73,9 +73,8 @@ def request(method, path, params, options)
end

options[:raw] ? response : response.body
rescue StandardError => error
error.extend(Twitter::Error)
raise
rescue Faraday::Error::ClientError
raise Twitter::Error::ClientError
end

end
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/response/parse_json.rb
Expand Up @@ -23,6 +23,7 @@ def on_complete(env)
env[:body] = parse(env[:body]) unless env[:request][:raw] || [204, 304].include?(env[:status])
end
end

end
end
end
4 changes: 2 additions & 2 deletions spec/faraday/response_spec.rb
Expand Up @@ -23,9 +23,9 @@

Twitter::Error::ClientError.errors.each do |status, exception|
[nil, "error", "errors"].each do |body|
context "when HTTP status is #{status} and body is #{body || 'nil'}" do
context "when HTTP status is #{status} and body is #{body.inspect}" do
before do
body_message = '{"' + body + '":"test"}' unless body.nil?
body_message = '{"' + body + '":"Client Error"}' unless body.nil?
stub_get("/1/statuses/user_timeline.json").
with(:query => {:screen_name => 'sferik'}).
to_return(:status => status, :body => body_message)
Expand Down
71 changes: 0 additions & 71 deletions spec/twitter/error/client_error_spec.rb

This file was deleted.

0 comments on commit 05d055d

Please sign in to comment.