Skip to content

Commit

Permalink
Catch and re-raise Faraday errors as Twitter::Error::ClientError
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 9, 2012
1 parent 32e3fde commit ccf3dde
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ The `Twitter::Status#expanded_urls` method has been removed. Use
Support for API gateways via `gateway` configuration has been removed. This
may still be implemented by inserting custom Faraday middleware.

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
you hit your rate limit, Twitter returns HTTP 400, which raises a
Expand Down
2 changes: 2 additions & 0 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def connection
builder.use Twitter::Response::RaiseServerError
builder.adapter adapter
end
rescue Faraday::Error::ClientError
raise Twitter::ClientError
end

end
Expand Down
19 changes: 14 additions & 5 deletions lib/twitter/error.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
module Twitter
# Custom error class for rescuing from all Twitter errors
class Error < StandardError
attr_reader :http_headers
attr_reader :http_headers, :wrapped_exception

# Initializes a new Error object
#
# @param message [String]
# @param exception [Exception, String]
# @param http_headers [Hash]
# @return [Twitter::Error]
def initialize(message, 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/client_error.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'twitter/error'

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

0 comments on commit ccf3dde

Please sign in to comment.