From 1ea83ce85b44b9566bcc336d529f418b1c89b61e Mon Sep 17 00:00:00 2001 From: Huang Chen Date: Mon, 18 Jan 2021 12:49:27 +0800 Subject: [PATCH] Catch exceptions in exec callback to ensure Promise is rejected --- index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d9b02b3..654c38d 100644 --- a/index.js +++ b/index.js @@ -68,13 +68,19 @@ export function machineIdSync(original: boolean): string { export function machineId(original: boolean): Promise { 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)); }); }); }