Skip to content

Commit

Permalink
Update check_status to better support different types of errors (goog…
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Wang authored and saicheems committed Feb 2, 2017
1 parent 88c22f7 commit e9d7e47
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/google/apis/core/api_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ module Core
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]
Expand Down Expand Up @@ -94,10 +98,12 @@ def check_status(status, header = nil, body = nil, message = nil)
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
Expand Down
4 changes: 4 additions & 0 deletions lib/google/apis/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class ClientError < Error
class RateLimitError < Error
end

# A 403 HTTP error occurred.
class ProjectNotLinkedError < Error
end

# A 401 HTTP error occurred.
class AuthorizationError < Error
end
Expand Down
38 changes: 38 additions & 0 deletions spec/google/apis/core/api_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,44 @@
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')
Expand Down

0 comments on commit e9d7e47

Please sign in to comment.