From d7092d68bba8e5e1fd140a147e3ae3a512520830 Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Fri, 22 Nov 2024 22:42:43 +0530 Subject: [PATCH] Fix unhandledRejection on error inside function-based custom commands. --- lib/api/_loaders/command.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/api/_loaders/command.js b/lib/api/_loaders/command.js index 3afd576d5b..1606d4688e 100644 --- a/lib/api/_loaders/command.js +++ b/lib/api/_loaders/command.js @@ -183,7 +183,16 @@ class CommandLoader extends BaseCommandLoader { }) .catch(err => { if (instance instanceof EventEmitter) { - instance.emit('error', err); + if (instance.needsPromise) { + // if the instance has `needsPromise` set to `true`, the `error` event is listened + // on the `context` object, not on the `instance` object (in `treenode.js`). + this.emit('error', err); + } else { + // most probably a dead code because all `instance` objects with `EventEmitter` + // superclass are expected to be function-style commands which will always have + // the `needsPromise` property set to `true`. But, not taking chances here. + instance.emit('error', err); + } return; }