Merge pull request #97 from serihiro/retry-request-with-unexpected-error
Retry http request when unexpected error occurs
This commit is contained in:
commit
8665196405
|
@ -88,16 +88,20 @@ END
|
||||||
def fetch_access_token(options = {})
|
def fetch_access_token(options = {})
|
||||||
c = options[:connection] || Faraday.default_connection
|
c = options[:connection] || Faraday.default_connection
|
||||||
c.headers = { 'Metadata-Flavor' => 'Google' }
|
c.headers = { 'Metadata-Flavor' => 'Google' }
|
||||||
resp = c.get(COMPUTE_AUTH_TOKEN_URI)
|
|
||||||
case resp.status
|
retry_with_error do
|
||||||
when 200
|
resp = c.get(COMPUTE_AUTH_TOKEN_URI)
|
||||||
Signet::OAuth2.parse_credentials(resp.body,
|
case resp.status
|
||||||
resp.headers['content-type'])
|
when 200
|
||||||
when 404
|
Signet::OAuth2.parse_credentials(resp.body,
|
||||||
raise(Signet::AuthorizationError, NO_METADATA_SERVER_ERROR)
|
resp.headers['content-type'])
|
||||||
else
|
when 404
|
||||||
msg = "Unexpected error code #{resp.status}" + UNEXPECTED_ERROR_SUFFIX
|
raise(Signet::AuthorizationError, NO_METADATA_SERVER_ERROR)
|
||||||
raise(Signet::AuthorizationError, msg)
|
else
|
||||||
|
msg = "Unexpected error code #{resp.status}" \
|
||||||
|
"#{UNEXPECTED_ERROR_SUFFIX}"
|
||||||
|
raise(Signet::AuthorizationError, msg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,6 +77,27 @@ module Signet
|
||||||
block.call(self)
|
block.call(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def retry_with_error(max_retry_count = 5)
|
||||||
|
retry_count = 0
|
||||||
|
|
||||||
|
begin
|
||||||
|
yield
|
||||||
|
rescue => e
|
||||||
|
if e.is_a?(Signet::AuthorizationError) || e.is_a?(Signet::ParseError)
|
||||||
|
raise e
|
||||||
|
end
|
||||||
|
|
||||||
|
if retry_count < max_retry_count
|
||||||
|
retry_count += 1
|
||||||
|
sleep retry_count * 0.3
|
||||||
|
retry
|
||||||
|
else
|
||||||
|
msg = "Unexpected error: #{e.inspect}"
|
||||||
|
raise(Signet::AuthorizationError, msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -92,15 +92,18 @@ module Google
|
||||||
# Revokes the credential
|
# Revokes the credential
|
||||||
def revoke!(options = {})
|
def revoke!(options = {})
|
||||||
c = options[:connection] || Faraday.default_connection
|
c = options[:connection] || Faraday.default_connection
|
||||||
resp = c.get(REVOKE_TOKEN_URI, token: refresh_token || access_token)
|
|
||||||
case resp.status
|
retry_with_error do
|
||||||
when 200
|
resp = c.get(REVOKE_TOKEN_URI, token: refresh_token || access_token)
|
||||||
self.access_token = nil
|
case resp.status
|
||||||
self.refresh_token = nil
|
when 200
|
||||||
self.expires_at = 0
|
self.access_token = nil
|
||||||
else
|
self.refresh_token = nil
|
||||||
raise(Signet::AuthorizationError,
|
self.expires_at = 0
|
||||||
"Unexpected error code #{resp.status}")
|
else
|
||||||
|
raise(Signet::AuthorizationError,
|
||||||
|
"Unexpected error code #{resp.status}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -77,6 +77,20 @@ describe Google::Auth::GCECredentials do
|
||||||
.to raise_error Signet::AuthorizationError
|
.to raise_error Signet::AuthorizationError
|
||||||
expect(stub).to have_been_requested
|
expect(stub).to have_been_requested
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should fail with Signet::AuthorizationError if request times out' do
|
||||||
|
allow_any_instance_of(Faraday::Connection).to receive(:get)
|
||||||
|
.and_raise(Faraday::TimeoutError)
|
||||||
|
expect { @client.fetch_access_token! }
|
||||||
|
.to raise_error Signet::AuthorizationError
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should fail with Signet::AuthorizationError if request fails' do
|
||||||
|
allow_any_instance_of(Faraday::Connection).to receive(:get)
|
||||||
|
.and_raise(Faraday::ConnectionFailed, nil)
|
||||||
|
expect { @client.fetch_access_token! }
|
||||||
|
.to raise_error Signet::AuthorizationError
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -293,4 +293,20 @@ describe Google::Auth::UserRefreshCredentials do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'when erros occured with request' do
|
||||||
|
it 'should fail with Signet::AuthorizationError if request times out' do
|
||||||
|
allow_any_instance_of(Faraday::Connection).to receive(:get)
|
||||||
|
.and_raise(Faraday::TimeoutError)
|
||||||
|
expect { @client.revoke! }
|
||||||
|
.to raise_error Signet::AuthorizationError
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should fail with Signet::AuthorizationError if request fails' do
|
||||||
|
allow_any_instance_of(Faraday::Connection).to receive(:get)
|
||||||
|
.and_raise(Faraday::ConnectionFailed, nil)
|
||||||
|
expect { @client.revoke! }
|
||||||
|
.to raise_error Signet::AuthorizationError
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue