Skip to content

Commit 08684cf

Browse files
saicheemsofrobots
authored andcommitted
Treat 4xx and 5xx responses as errors (googleapis#149)
* Treat 4xx and 5xx responses as errors At the moment, 4xx and 5xx responses are returned in google-api-nodejs-client as regular responses, not errors. From the user perspective, this is an odd pattern. This commit forces 4xx and 5xx responses to be returned as errors, and not be treated as a successful requests.
1 parent bd13376 commit 08684cf

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

ts/lib/auth/oauth2client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ export default class OAuth2Client extends AuthClient {
297297

298298
return this.refreshToken(
299299
thisCreds.refresh_token, (err, tokens, response) => {
300-
if (err) {
300+
// If the error code is 403 or 404, go to the else so the error
301+
// message is replaced. Otherwise, return the error.
302+
if (err && (err as RequestError).code != 403 &&
303+
(err as RequestError).code != 404) {
301304
return metadataCb(err, null, response);
302305
} else {
303306
if (!tokens || (tokens && !tokens.access_token)) {

ts/lib/transporters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export class DefaultTransporter {
103103
(err as RequestError).code = body.error.code || res.statusCode;
104104
}
105105
body = null;
106-
} else if (res.statusCode >= 500) {
107-
// Consider all '500 responses' errors.
106+
} else if (res.statusCode >= 400) {
107+
// Consider all 4xx and 5xx responses errors.
108108
err = new RequestError(body);
109109
(err as RequestError).code = res.statusCode;
110110
body = null;

ts/test/test.transporters.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,18 @@ describe('Transporters', () => {
7171
done();
7272
});
7373
});
74+
75+
it('should return an error for a 404 response', (done) => {
76+
nock('http://example.com').get('/api').reply(404, 'Not found');
77+
78+
transporter.request(
79+
{
80+
uri: 'http://example.com/api',
81+
},
82+
(error) => {
83+
assert(error.message === 'Not found');
84+
assert.equal((error as RequestError).code, 404);
85+
done();
86+
});
87+
});
7488
});

0 commit comments

Comments
 (0)