diff --git a/lib/google/apis/core/api_command.rb b/lib/google/apis/core/api_command.rb index 5715d2f00..abcc7fb67 100644 --- a/lib/google/apis/core/api_command.rb +++ b/lib/google/apis/core/api_command.rb @@ -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 diff --git a/lib/google/apis/errors.rb b/lib/google/apis/errors.rb index 29fed01de..0c8d053cf 100644 --- a/lib/google/apis/errors.rb +++ b/lib/google/apis/errors.rb @@ -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 diff --git a/spec/google/apis/core/api_command_spec.rb b/spec/google/apis/core/api_command_spec.rb index 40ed3b90e..84b9629d3 100644 --- a/spec/google/apis/core/api_command_spec.rb +++ b/spec/google/apis/core/api_command_spec.rb @@ -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 = < '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')