Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: surface original stack trace and message with errors #651

Merged
merged 2 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auth/computeclient.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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