Skip to content

Catch exceptions in exec callback to ensure Promise is rejected #49

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ export function machineIdSync(original: boolean): string {
export function machineId(original: boolean): Promise<string> {
return new Promise((resolve: Function, reject: Function): Object => {
return exec(guid[platform], {}, (err: any, stdout: any, stderr: any) => {
if (err) {
return reject(
new Error(`Error while obtaining machine id: ${err.stack}`)
);
// This is executing in a callback, so any Exceptions thrown will
// not reject the promise
try {
if (err) {
return reject(
new Error(`Error while obtaining machine id: ${err.stack}`)
);
}
let id: string = expose(stdout.toString());
return resolve(original ? id : hash(id));
} catch (exception) {
return reject(exception);
}
let id: string = expose(stdout.toString());
return resolve(original ? id : hash(id));
});
});
}