Skip to content

Commit

Permalink
fix: surface original stack trace and message with errors (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 27, 2019
1 parent 5b74985 commit 8fb65eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/auth/computeclient.ts
Expand Up @@ -70,7 +70,7 @@ export class Compute extends OAuth2Client {
try {
data = await gcpMetadata.instance(tokenPath);
} catch (e) {
e.message = 'Could not refresh access token.';
e.message = `Could not refresh access token: ${e.message}`;
throw e;
}
const tokens = data as Credentials;
Expand Down
41 changes: 12 additions & 29 deletions src/auth/googleauth.ts
Expand Up @@ -258,8 +258,9 @@ export class GoogleAuth {
try {
isGCE = await this._checkIsGCE();
} catch (e) {
throw new Error(
'Unexpected error determining execution environment: ' + e.message);
e.message =
`Unexpected error determining execution environment: ${e.message}`;
throw e;
}

if (!isGCE) {
Expand Down Expand Up @@ -303,9 +304,10 @@ export class GoogleAuth {
return this._getApplicationCredentialsFromFilePath(
credentialsPath, options);
} catch (e) {
throw this.createError(
'Unable to read the credential file specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable.',
e);
e.message =
`Unable to read the credential file specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable: ${
e.message}`;
throw e;
}
}

Expand Down Expand Up @@ -375,17 +377,14 @@ export class GoogleAuth {
throw new Error();
}
} catch (err) {
throw this.createError(
`The file at ${filePath} does not exist, or it is not a file.`, err);
err.message = `The file at ${
filePath} does not exist, or it is not a file. ${err.message}`;
throw err;
}

// Now open a read stream on the file, and parse it.
try {
const readStream = this._createReadStream(filePath);
return this.fromStream(readStream, options);
} catch (err) {
throw this.createError(`Unable to read the file at ${filePath}.`, err);
}
const readStream = this._createReadStream(filePath);
return this.fromStream(readStream, options);
}

/**
Expand Down Expand Up @@ -544,22 +543,6 @@ export class GoogleAuth {
return filePath;
}

// Creates an Error containing the given message, and includes the message
// from the optional err passed in.
private createError(message: string, err: Error) {
let s = message || '';
if (err) {
const errorMessage = String(err);
if (errorMessage && errorMessage.length > 0) {
if (s.length > 0) {
s += ' ';
}
s += errorMessage;
}
}
return Error(s);
}

/**
* Run the Google Cloud SDK command that prints the default project ID
*/
Expand Down
12 changes: 8 additions & 4 deletions src/auth/oauth2client.ts
Expand Up @@ -718,7 +718,7 @@ export class OAuth2Client extends AuthClient {
const e = err as GaxiosError;
if (e.response &&
(e.response.status === 403 || e.response.status === 404)) {
e.message = 'Could not refresh access token.';
e.message = `Could not refresh access token: ${e.message}`;
}
throw e;
}
Expand Down Expand Up @@ -962,7 +962,8 @@ export class OAuth2Client extends AuthClient {
try {
res = await this.transporter.request({url});
} catch (e) {
throw new Error('Failed to retrieve verification certificates: ' + e);
e.message = `Failed to retrieve verification certificates: ${e.message}`;
throw e;
}

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

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

if (!payload) {
Expand Down

0 comments on commit 8fb65eb

Please sign in to comment.