Update check_status to better support different types of errors (#517)

This commit is contained in:
Austin Wang 2017-02-01 23:20:19 -08:00 committed by Sai Cheemalapati
parent 88c22f75e7
commit e9d7e47c75
3 changed files with 53 additions and 5 deletions

View File

@ -26,7 +26,11 @@ module Google
class ApiCommand < HttpCommand
JSON_CONTENT_TYPE = 'application/json'
FIELDS_PARAM = 'fields'
RATE_LIMIT_ERRORS = %w(rateLimitExceeded userRateLimitExceeded)
ERROR_REASON_MAPPING = {
'rateLimitExceeded' => Google::Apis::RateLimitError,
'userRateLimitExceeded' => Google::Apis::RateLimitError,
'projectNotLinked' => Google::Apis::ProjectNotLinkedError
}
# JSON serializer for request objects
# @return [Google::Apis::Core::JsonRepresentation]
@ -94,10 +98,12 @@ module Google
error = parse_error(body)
if error
message = sprintf('%s: %s', error['reason'], error['message'])
raise Google::Apis::RateLimitError.new(message,
status_code: status,
header: header,
body: body) if RATE_LIMIT_ERRORS.include?(error['reason'])
raise ERROR_REASON_MAPPING[error['reason']].new(
message,
status_code: status,
header: header,
body: body
) if ERROR_REASON_MAPPING.key?(error['reason'])
end
super(status, header, body, message)
else

View File

@ -61,6 +61,10 @@ module Google
class RateLimitError < Error
end
# A 403 HTTP error occurred.
class ProjectNotLinkedError < Error
end
# A 401 HTTP error occurred.
class AuthorizationError < Error
end

View File

@ -159,6 +159,44 @@ EOF
end
end
context('with a project not linked response') do
let(:command) do
Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
end
before(:example) do
json = <<EOF
{
"error": {
"errors": [
{
"domain": "global",
"reason": "projectNotLinked",
"message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
}
],
"code": 403,
"message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
}
}
EOF
stub_request(:get, 'https://www.googleapis.com/zoo/animals')
.to_return(status: [403, 'The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.'], headers: {
'Content-Type' => 'application/json'
}, body: json)
.to_return(headers: { 'Content-Type' => 'application/json' }, body: %({}))
end
it 'should raise project not linked error' do
expect { command.execute(client) }.to raise_error(Google::Apis::ProjectNotLinkedError)
end
it 'should raise an error with the reason and message' do
expect { command.execute(client) }.to raise_error(
/projectNotLinked: The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console./)
end
end
context('with a client error response') do
let(:command) do
Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')