Browse files

Improved error handling by separating HTTP 4xx errors from HTTP 5xx e…

…rrors, so HTTP 4xx errors can be parsed first.

See http://github.com/jnunemaker/twitter/commit/6d76a3ae906bd36d78c3adbf5e79e61cefd0b580#comments for details.
  • Loading branch information...
1 parent 54c4b36 commit f26e7875980a7b2b16285c31198601b92ac5cbb6 @sferik committed Oct 14, 2010
View
2 HISTORY.rdoc
@@ -1,7 +1,7 @@
=== 1.0.0.rc.1 - October 12, 2010
* {Switch from httparty to faraday HTTP client library}[http://github.com/jnunemaker/twitter/commit/80aff88dae11d64673fe4e025cc8f065a6796345]
* {Switch from oauth to simple_oauth for authentication}[http://github.com/jnunemaker/twitter/commit/76cfe3749e56b2b486f2b5ffc9aa7f437cb2db29] (@laserlemon[http://twitter.com/#!/laserlemon])
-* {Handle errors with <tt>faraday_middleware</tt>}[http://github.com/jnunemaker/twitter/commit/1f99e46f602268bc82daf39b329d81e54abfb292]
+* {Handle errors in faraday middleware}[http://github.com/jnunemaker/twitter/commit/466a0d9942d1c0c0c35c6302951087076ddf4b82#diff-2]
* {Add #NewTwitter methods and tests}[http://github.com/jnunemaker/twitter/commit/0bfbf6352de9bdda2b93ed053a358c0cb8e78e8f]
* {Fix tests that assume position in a <tt>Hash</tt>}[http://github.com/jnunemaker/twitter/commit/c9f7ed1d9106807aa6fb27d48a92f4b92d0594a7] (@duncan[http://twitter.com/#!/duncan])
* {Enable SSL by default (add option to disable SSL)}[http://github.com/jnunemaker/twitter/commit/c4f8907d6595f93d63bc84d6575920a14774e656]
View
33 lib/faraday/raise_errors.rb
@@ -1,33 +0,0 @@
-module Faraday
- class Response::RaiseErrors < Response::Middleware
- def self.register_on_complete(env)
- env[:response].on_complete do |response|
- case response[:status].to_i
- when 400
- raise Twitter::BadRequest, "#{response[:body]['error'] if response[:body]}"
- when 401
- raise Twitter::Unauthorized, "#{response[:body]['error'] if response[:body]}"
- when 403
- raise Twitter::Forbidden, "#{response[:body]['error'] if response[:body]}"
- when 404
- raise Twitter::NotFound, "#{response[:body]['error'] if response[:body]}"
- when 406
- raise Twitter::NotAcceptable, "#{response[:body]['error'] if response[:body]}"
- when 420
- raise Twitter::EnhanceYourCalm, "#{response[:body]['error'] if response[:body]}"
- when 500
- raise Twitter::InternalServerError, "#{response[:body]['error'] if response[:body]}"
- when 502
- raise Twitter::BadGateway, "#{response[:body]['error'] if response[:body]}"
- when 503
- raise Twitter::ServiceUnavailable, "#{response[:body]['error'] if response[:body]}"
- end
- end
- end
-
- def initialize(app)
- super
- @parser = nil
- end
- end
-end
View
33 lib/faraday/raise_http_4xx.rb
@@ -0,0 +1,33 @@
+module Faraday
+ class Response::RaiseHttp4xx < Response::Middleware
+ def self.register_on_complete(env)
+ env[:response].on_complete do |response|
+ case response[:status].to_i
+ when 400
+ raise Twitter::BadRequest, error_message(response)
+ when 401
+ raise Twitter::Unauthorized, error_message(response)
+ when 403
+ raise Twitter::Forbidden, error_message(response)
+ when 404
+ raise Twitter::NotFound, error_message(response)
+ when 406
+ raise Twitter::NotAcceptable, error_message(response)
+ when 420
+ raise Twitter::EnhanceYourCalm, error_message(response)
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+
+ private
+
+ def self.error_message(response)
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
+ end
+ end
+end
View
27 lib/faraday/raise_http_5xx.rb
@@ -0,0 +1,27 @@
+module Faraday
+ class Response::RaiseHttp5xx < Response::Middleware
+ def self.register_on_complete(env)
+ env[:response].on_complete do |response|
+ case response[:status].to_i
+ when 500
+ raise Twitter::InternalServerError, error_message(response, "Something is technically wrong.")
+ when 502
+ raise Twitter::BadGateway, error_message(response, "Twitter is down or being upgraded.")
+ when 503
+ raise Twitter::ServiceUnavailable, error_message(response, "(__-){ Twitter is over capacity.")
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+
+ private
+
+ def self.error_message(response, body=nil)
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{': ' + body if body} Check http://status.twitter.com/ for update on the status of the Twitter service."
+ end
+ end
+end
View
6 lib/twitter/base.rb
@@ -441,20 +441,22 @@ def build_multipart_bodies(parts) self.class.build_multipart_bodies(parts) end
def connection
builders = []
- builders << Faraday::Response::RaiseErrors
+ builders << Faraday::Response::RaiseHttp5xx
case Twitter.format.to_s
when "json"
builders << Faraday::Response::ParseJson
when "xml"
builders << Faraday::Response::ParseXml
end
+ builders << Faraday::Response::RaiseHttp4xx
builders << Faraday::Response::Mashify
connection_with_builders(builders)
end
def connection_with_unparsed_response
builders = []
- builders << Faraday::Response::RaiseErrors
+ builders << Faraday::Response::RaiseHttp5xx
+ builders << Faraday::Response::RaiseHttp4xx
connection_with_builders(builders)
end
View
3 lib/twitter/geo.rb
@@ -127,13 +127,14 @@ def connection
ssl = {:verify => false}
@connection = Faraday::Connection.new(:url => @api_endpoint, :headers => headers, :ssl => ssl) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
- builder.use Faraday::Response::RaiseErrors
+ builder.use Faraday::Response::RaiseHttp5xx
case Twitter.format.to_s
when "json"
builder.use Faraday::Response::ParseJson
when "xml"
builder.use Faraday::Response::ParseXml
end
+ builder.use Faraday::Response::RaiseHttp4xx
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.protocol
View
3 lib/twitter/search.rb
@@ -275,13 +275,14 @@ def connection
ssl = {:verify => false}
@connection = Faraday::Connection.new(:url => @api_endpoint.omit(:path), :headers => headers, :ssl => false) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
- builder.use Faraday::Response::RaiseErrors
+ builder.use Faraday::Response::RaiseHttp5xx
case Twitter.format.to_s
when "json"
builder.use Faraday::Response::ParseJson
when "xml"
builder.use Faraday::Response::ParseXml
end
+ builder.use Faraday::Response::RaiseHttp4xx
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.protocol
View
3 lib/twitter/trends.rb
@@ -101,13 +101,14 @@ def connection
ssl = {:verify => false}
@connection = Faraday::Connection.new(:url => @api_endpoint, :headers => headers, :ssl => ssl) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
- builder.use Faraday::Response::RaiseErrors
+ builder.use Faraday::Response::RaiseHttp5xx
case Twitter.format.to_s
when "json"
builder.use Faraday::Response::ParseJson
when "xml"
builder.use Faraday::Response::ParseXml
end
+ builder.use Faraday::Response::RaiseHttp4xx
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.protocol
View
6 lib/twitter/unauthenticated.rb
@@ -102,20 +102,22 @@ def list_timeline(list_owner_screen_name, slug, options = {})
def connection
builders = []
- builders << Faraday::Response::RaiseErrors
+ builders << Faraday::Response::RaiseHttp5xx
case Twitter.format.to_s
when "json"
builders << Faraday::Response::ParseJson
when "xml"
builders << Faraday::Response::ParseXml
end
+ builders << Faraday::Response::RaiseHttp4xx
builders << Faraday::Response::Mashify
connection_with_builders(builders)
end
def connection_with_unparsed_response
builders = []
- builders << Faraday::Response::RaiseErrors
+ builders << Faraday::Response::RaiseHttp5xx
+ builders << Faraday::Response::RaiseHttp4xx
connection_with_builders(builders)
end

1 comment on commit f26e787

@laserlemon
Collaborator

Love the fail whale!

Please sign in to comment.