-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
75 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f26e787
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the fail whale!