-
-
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.
- Loading branch information
Showing
9 changed files
with
89 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,45 @@ | ||
require 'faraday' | ||
|
||
# @private | ||
module Faraday | ||
# @private | ||
class Response::RaiseHttp4xx < Response::Middleware | ||
def on_complete(env) | ||
case env[:status].to_i | ||
when 400 | ||
raise Twitter::BadRequest.new(error_message(env), env[:response_headers]) | ||
when 401 | ||
raise Twitter::Unauthorized.new(error_message(env), env[:response_headers]) | ||
when 403 | ||
raise Twitter::Forbidden.new(error_message(env), env[:response_headers]) | ||
when 404 | ||
raise Twitter::NotFound.new(error_message(env), env[:response_headers]) | ||
when 406 | ||
raise Twitter::NotAcceptable.new(error_message(env), env[:response_headers]) | ||
when 420 | ||
raise Twitter::EnhanceYourCalm.new(error_message(env), env[:response_headers]) | ||
end | ||
end | ||
|
||
private | ||
|
||
def error_message(env) | ||
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}" | ||
end | ||
|
||
def error_body(body) | ||
if body.nil? | ||
nil | ||
elsif body['error'] | ||
": #{body['error']}" | ||
elsif body['errors'] | ||
first = body['errors'].to_a.first | ||
if first.kind_of? Hash | ||
": #{first['message'].chomp}" | ||
else | ||
": #{first.chomp}" | ||
end | ||
end | ||
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,24 @@ | ||
require 'faraday' | ||
|
||
# @private | ||
module Faraday | ||
# @private | ||
class Response::RaiseHttp5xx < Response::Middleware | ||
def on_complete(env) | ||
case env[:status].to_i | ||
when 500 | ||
raise Twitter::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers]) | ||
when 502 | ||
raise Twitter::BadGateway.new(error_message(env, "Twitter is down or being upgraded."), env[:response_headers]) | ||
when 503 | ||
raise Twitter::ServiceUnavailable.new(error_message(env, "(__-){ Twitter is over capacity."), env[:response_headers]) | ||
end | ||
end | ||
|
||
private | ||
|
||
def error_message(env, body=nil) | ||
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')} Check http://status.twitter.com/ for updates 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