Skip to content

Commit

Permalink
Improved error handling by separating HTTP 4xx errors from HTTP 5xx e…
Browse files Browse the repository at this point in the history
…rrors, so HTTP 4xx errors can be parsed first.

See http://github.com/jnunemaker/twitter/commit/6d76a3ae906bd36d78c3adbf5e79e61cefd0b580#comments for details.
  • Loading branch information
sferik committed Oct 14, 2010
1 parent 54c4b36 commit f26e787
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 41 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rdoc
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
33 changes: 0 additions & 33 deletions lib/faraday/raise_errors.rb

This file was deleted.

33 changes: 33 additions & 0 deletions lib/faraday/raise_http_4xx.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions lib/faraday/raise_http_5xx.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/twitter/unauthenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

1 comment on commit f26e787

@laserlemon
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the fail whale!

Please sign in to comment.