Include reason & message in API error when available
This commit is contained in:
parent
2336c1a61a
commit
451a0b6617
|
@ -94,11 +94,11 @@ module Google
|
|||
when 400, 402...500
|
||||
error = parse_error(body)
|
||||
if error
|
||||
message = error['reason'] if error.has_key?('reason')
|
||||
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?(message)
|
||||
body: body) if RATE_LIMIT_ERRORS.include?(error['reason'])
|
||||
end
|
||||
super(status, header, body, message)
|
||||
else
|
||||
|
|
|
@ -151,6 +151,41 @@ EOF
|
|||
end
|
||||
end
|
||||
|
||||
context('with a client error 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": "timeRangeEmpty",
|
||||
"message": "The specified time range is empty."
|
||||
}
|
||||
],
|
||||
"code": 400,
|
||||
"message": "The specified time range is empty."
|
||||
}
|
||||
}
|
||||
EOF
|
||||
stub_request(:get, 'https://www.googleapis.com/zoo/animals')
|
||||
.to_return(status: [400, 'Bad Request'], headers: { 'Content-Type' => 'application/json' }, body: json)
|
||||
end
|
||||
|
||||
it 'should raise client error' do
|
||||
expect { command.execute(client) }.to raise_error(Google::Apis::ClientError)
|
||||
end
|
||||
|
||||
it 'should raise an error with the reason and message' do
|
||||
expect { command.execute(client) }.to raise_error(
|
||||
/timeRangeEmpty: The specified time range is empty/)
|
||||
end
|
||||
end
|
||||
|
||||
context('with an empty error body') do
|
||||
let(:command) do
|
||||
Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
|
||||
|
@ -166,5 +201,9 @@ EOF
|
|||
it 'should raise client error' do
|
||||
expect { command.execute(client) }.to raise_error(Google::Apis::ClientError)
|
||||
end
|
||||
|
||||
it 'should use the default error message' do
|
||||
expect { command.execute(client) }.to raise_error(/Invalid request/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,17 @@
|
|||
# Copyright 2015 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require 'spec_helper'
|
||||
require 'google/apis/urlshortener_v1'
|
||||
require 'googleauth'
|
||||
|
|
Loading…
Reference in New Issue