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

Commit 8fb65eb

Browse files
fix: surface original stack trace and message with errors (#651)
1 parent 5b74985 commit 8fb65eb

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

src/auth/computeclient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class Compute extends OAuth2Client {
7070
try {
7171
data = await gcpMetadata.instance(tokenPath);
7272
} catch (e) {
73-
e.message = 'Could not refresh access token.';
73+
e.message = `Could not refresh access token: ${e.message}`;
7474
throw e;
7575
}
7676
const tokens = data as Credentials;

src/auth/googleauth.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ export class GoogleAuth {
258258
try {
259259
isGCE = await this._checkIsGCE();
260260
} catch (e) {
261-
throw new Error(
262-
'Unexpected error determining execution environment: ' + e.message);
261+
e.message =
262+
`Unexpected error determining execution environment: ${e.message}`;
263+
throw e;
263264
}
264265

265266
if (!isGCE) {
@@ -303,9 +304,10 @@ export class GoogleAuth {
303304
return this._getApplicationCredentialsFromFilePath(
304305
credentialsPath, options);
305306
} catch (e) {
306-
throw this.createError(
307-
'Unable to read the credential file specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable.',
308-
e);
307+
e.message =
308+
`Unable to read the credential file specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable: ${
309+
e.message}`;
310+
throw e;
309311
}
310312
}
311313

@@ -375,17 +377,14 @@ export class GoogleAuth {
375377
throw new Error();
376378
}
377379
} catch (err) {
378-
throw this.createError(
379-
`The file at ${filePath} does not exist, or it is not a file.`, err);
380+
err.message = `The file at ${
381+
filePath} does not exist, or it is not a file. ${err.message}`;
382+
throw err;
380383
}
381384

382385
// Now open a read stream on the file, and parse it.
383-
try {
384-
const readStream = this._createReadStream(filePath);
385-
return this.fromStream(readStream, options);
386-
} catch (err) {
387-
throw this.createError(`Unable to read the file at ${filePath}.`, err);
388-
}
386+
const readStream = this._createReadStream(filePath);
387+
return this.fromStream(readStream, options);
389388
}
390389

391390
/**
@@ -544,22 +543,6 @@ export class GoogleAuth {
544543
return filePath;
545544
}
546545

547-
// Creates an Error containing the given message, and includes the message
548-
// from the optional err passed in.
549-
private createError(message: string, err: Error) {
550-
let s = message || '';
551-
if (err) {
552-
const errorMessage = String(err);
553-
if (errorMessage && errorMessage.length > 0) {
554-
if (s.length > 0) {
555-
s += ' ';
556-
}
557-
s += errorMessage;
558-
}
559-
}
560-
return Error(s);
561-
}
562-
563546
/**
564547
* Run the Google Cloud SDK command that prints the default project ID
565548
*/

src/auth/oauth2client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ export class OAuth2Client extends AuthClient {
718718
const e = err as GaxiosError;
719719
if (e.response &&
720720
(e.response.status === 403 || e.response.status === 404)) {
721-
e.message = 'Could not refresh access token.';
721+
e.message = `Could not refresh access token: ${e.message}`;
722722
}
723723
throw e;
724724
}
@@ -962,7 +962,8 @@ export class OAuth2Client extends AuthClient {
962962
try {
963963
res = await this.transporter.request({url});
964964
} catch (e) {
965-
throw new Error('Failed to retrieve verification certificates: ' + e);
965+
e.message = `Failed to retrieve verification certificates: ${e.message}`;
966+
throw e;
966967
}
967968

968969
const cacheControl = res ? res.headers['cache-control'] : undefined;
@@ -1037,7 +1038,9 @@ export class OAuth2Client extends AuthClient {
10371038
try {
10381039
envelope = JSON.parse(crypto.decodeBase64StringUtf8(segments[0]));
10391040
} catch (err) {
1040-
throw new Error('Can\'t parse token envelope: ' + segments[0]);
1041+
err.message =
1042+
`Can't parse token envelope: ${segments[0]}': ${err.message}`;
1043+
throw err;
10411044
}
10421045

10431046
if (!envelope) {
@@ -1047,7 +1050,8 @@ export class OAuth2Client extends AuthClient {
10471050
try {
10481051
payload = JSON.parse(crypto.decodeBase64StringUtf8(segments[1]));
10491052
} catch (err) {
1050-
throw new Error('Can\'t parse token payload: ' + segments[0]);
1053+
err.message = `Can't parse token payload '${segments[0]}`;
1054+
throw err;
10511055
}
10521056

10531057
if (!payload) {

0 commit comments

Comments
 (0)