Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit dffd1cc

Browse files
fix: re-throw original exception and preserve message in compute client (#668)
1 parent b11841e commit dffd1cc

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

src/auth/computeclient.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class Compute extends OAuth2Client {
7171
data = await gcpMetadata.instance(tokenPath);
7272
} catch (e) {
7373
e.message = `Could not refresh access token: ${e.message}`;
74+
this.wrapError(e);
7475
throw e;
7576
}
7677
const tokens = data as Credentials;
@@ -82,36 +83,23 @@ export class Compute extends OAuth2Client {
8283
return {tokens, res: null};
8384
}
8485

85-
protected requestAsync<T>(opts: GaxiosOptions, retry = false):
86-
GaxiosPromise<T> {
87-
return super.requestAsync<T>(opts, retry).catch(e => {
88-
const res = (e as GaxiosError).response;
89-
if (res && res.status) {
90-
let helpfulMessage = null;
91-
if (res.status === 403) {
92-
helpfulMessage =
93-
'A Forbidden error was returned while attempting to retrieve an access ' +
94-
'token for the Compute Engine built-in service account. This may be because the Compute ' +
95-
'Engine instance does not have the correct permission scopes specified.';
96-
} else if (res.status === 404) {
97-
helpfulMessage =
98-
'A Not Found error was returned while attempting to retrieve an access' +
99-
'token for the Compute Engine built-in service account. This may be because the Compute ' +
100-
'Engine instance does not have any permission scopes specified.';
101-
}
102-
if (helpfulMessage) {
103-
if (e && e.message && !retry) {
104-
helpfulMessage += ' ' + e.message;
105-
}
106-
if (e) {
107-
e.message = helpfulMessage;
108-
} else {
109-
e = new Error(helpfulMessage);
110-
(e as NodeJS.ErrnoException).code = res.status.toString();
111-
}
112-
}
86+
protected wrapError(e: GaxiosError) {
87+
const res = e.response;
88+
if (res && res.status) {
89+
e.code = res.status.toString();
90+
if (res.status === 403) {
91+
e.message =
92+
'A Forbidden error was returned while attempting to retrieve an access ' +
93+
'token for the Compute Engine built-in service account. This may be because the Compute ' +
94+
'Engine instance does not have the correct permission scopes specified: ' +
95+
e.message;
96+
} else if (res.status === 404) {
97+
e.message =
98+
'A Not Found error was returned while attempting to retrieve an access' +
99+
'token for the Compute Engine built-in service account. This may be because the Compute ' +
100+
'Engine instance does not have any permission scopes specified: ' +
101+
e.message;
113102
}
114-
throw e;
115-
});
103+
}
116104
}
117105
}

test/test.compute.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ function mockExample() {
3737
}
3838

3939
// set up compute client.
40-
let sandbox: sinon.SinonSandbox;
40+
const sandbox = sinon.createSandbox();
4141
let compute: Compute;
4242
beforeEach(() => {
4343
compute = new Compute();
44-
sandbox = sinon.createSandbox();
4544
});
4645

4746
afterEach(() => {
@@ -129,37 +128,22 @@ it('should return false for createScopedRequired', () => {
129128
});
130129

131130
it('should return a helpful message on request response.statusCode 403', async () => {
132-
// Mock the credentials object. Make sure there's no expiry_date set.
133-
compute.credentials = {refresh_token: 'hello', access_token: 'goodbye'};
134-
135-
const scopes = [
136-
nock(url).get('/').reply(403), nock(HOST_ADDRESS).get(tokenPath).reply(403)
137-
];
138-
131+
const scope = mockToken(403);
139132
const expected = new RegExp(
140133
'A Forbidden error was returned while attempting to retrieve an access ' +
141134
'token for the Compute Engine built-in service account. This may be because the ' +
142135
'Compute Engine instance does not have the correct permission scopes specified. ' +
143136
'Could not refresh access token.');
144-
145137
await assertRejects(compute.request({url}), expected);
146-
scopes.forEach(s => s.done());
138+
scope.done();
147139
});
148140

149141
it('should return a helpful message on request response.statusCode 404', async () => {
150-
// Mock the credentials object.
151-
compute.credentials = {
152-
refresh_token: 'hello',
153-
access_token: 'goodbye',
154-
expiry_date: (new Date(9999, 1, 1)).getTime()
155-
};
156-
// Mock the request method to return a 404.
157-
const scope = nock(url).get('/').reply(404);
142+
const scope = mockToken(404);
158143
const expected = new RegExp(
159144
'A Not Found error was returned while attempting to retrieve an access' +
160145
'token for the Compute Engine built-in service account. This may be because the ' +
161146
'Compute Engine instance does not have any permission scopes specified.');
162-
163147
await assertRejects(compute.request({url}), expected);
164148
scope.done();
165149
});

0 commit comments

Comments
 (0)