From 2578736b44e2fb5234834a5d531c8063004ca41a Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Wed, 17 Aug 2016 13:58:07 -0700 Subject: [PATCH] #445 - Treat 429 status codes as rate limit errors --- lib/google/apis/core/http_command.rb | 3 +++ spec/google/apis/core/http_command_spec.rb | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/google/apis/core/http_command.rb b/lib/google/apis/core/http_command.rb index fb57d0f00..9e376660b 100644 --- a/lib/google/apis/core/http_command.rb +++ b/lib/google/apis/core/http_command.rb @@ -203,6 +203,9 @@ module Google when 401 message ||= 'Unauthorized' raise Google::Apis::AuthorizationError.new(message, status_code: status, header: header, body: body) + when 429 + message ||= 'Rate limit exceeded' + raise Google::Apis::RateLimitError.new(message, status_code: status, header: header, body: body) when 304, 400, 402...500 message ||= 'Invalid request' raise Google::Apis::ClientError.new(message, status_code: status, header: header, body: body) diff --git a/spec/google/apis/core/http_command_spec.rb b/spec/google/apis/core/http_command_spec.rb index b64a638b7..22e78e2ab 100644 --- a/spec/google/apis/core/http_command_spec.rb +++ b/spec/google/apis/core/http_command_spec.rb @@ -299,4 +299,11 @@ RSpec.describe Google::Apis::Core::HttpCommand do command.options.retries = 0 expect { command.execute(client) }.to raise_error(Google::Apis::TransmissionError) end + + it 'should raise rate limit error for 429 status codes' do + stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(status: [429, '']) + command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals') + command.options.retries = 0 + expect { command.execute(client) }.to raise_error(Google::Apis::RateLimitError) + end end