Permalink
Browse files

Catch and re-raise Faraday errors as Twitter::Error::ClientError

  • Loading branch information...
1 parent 32e3fde commit ccf3ddeb4cae937fdf3335546c17884472855149 @sferik committed Jun 9, 2012
Showing with 21 additions and 6 deletions.
  1. +4 −0 README.md
  2. +2 −0 lib/twitter/connection.rb
  3. +14 −5 lib/twitter/error.rb
  4. +1 −1 lib/twitter/error/client_error.rb
View
@@ -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
@@ -36,6 +36,8 @@ def connection
builder.use Twitter::Response::RaiseServerError
builder.adapter adapter
end
+ rescue Faraday::Error::ClientError
+ raise Twitter::ClientError
end
end
View
@@ -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]
@@ -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.