Update check_status to better support different types of errors (#517)
This commit is contained in:
parent
88c22f75e7
commit
e9d7e47c75
|
@ -26,7 +26,11 @@ module Google
|
||||||
class ApiCommand < HttpCommand
|
class ApiCommand < HttpCommand
|
||||||
JSON_CONTENT_TYPE = 'application/json'
|
JSON_CONTENT_TYPE = 'application/json'
|
||||||
FIELDS_PARAM = 'fields'
|
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
|
# JSON serializer for request objects
|
||||||
# @return [Google::Apis::Core::JsonRepresentation]
|
# @return [Google::Apis::Core::JsonRepresentation]
|
||||||
|
@ -94,10 +98,12 @@ module Google
|
||||||
error = parse_error(body)
|
error = parse_error(body)
|
||||||
if error
|
if error
|
||||||
message = sprintf('%s: %s', error['reason'], error['message'])
|
message = sprintf('%s: %s', error['reason'], error['message'])
|
||||||
raise Google::Apis::RateLimitError.new(message,
|
raise ERROR_REASON_MAPPING[error['reason']].new(
|
||||||
|
message,
|
||||||
status_code: status,
|
status_code: status,
|
||||||
header: header,
|
header: header,
|
||||||
body: body) if RATE_LIMIT_ERRORS.include?(error['reason'])
|
body: body
|
||||||
|
) if ERROR_REASON_MAPPING.key?(error['reason'])
|
||||||
end
|
end
|
||||||
super(status, header, body, message)
|
super(status, header, body, message)
|
||||||
else
|
else
|
||||||
|
|
|
@ -61,6 +61,10 @@ module Google
|
||||||
class RateLimitError < Error
|
class RateLimitError < Error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A 403 HTTP error occurred.
|
||||||
|
class ProjectNotLinkedError < Error
|
||||||
|
end
|
||||||
|
|
||||||
# A 401 HTTP error occurred.
|
# A 401 HTTP error occurred.
|
||||||
class AuthorizationError < Error
|
class AuthorizationError < Error
|
||||||
end
|
end
|
||||||
|
|
|
@ -159,6 +159,44 @@ EOF
|
||||||
end
|
end
|
||||||
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
|
context('with a client error response') do
|
||||||
let(:command) do
|
let(:command) do
|
||||||
Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
|
Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
|
||||||
|
|
Loading…
Reference in New Issue