From 1d6e122f0a092bd91fd95f89934ec2eb506e74cf Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Tue, 26 Nov 2024 01:38:49 +0530 Subject: [PATCH] Fix `unhandledRejection` on error inside function-based custom commands. (#4309) --- lib/api/_loaders/command.js | 11 ++++++++++- lib/core/treenode.js | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/api/_loaders/command.js b/lib/api/_loaders/command.js index 3afd576d5b..da1ac05795 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 { + // for class-based commands that inherit from EventEmitter. + // Since the `needsPromise` is set to `false` in this case, the `complete` and `error` + // events are listened on the `instance` object. + instance.emit('error', err); + } return; } diff --git a/lib/core/treenode.js b/lib/core/treenode.js index 1bfa4ae365..057cbb0fba 100644 --- a/lib/core/treenode.js +++ b/lib/core/treenode.js @@ -206,6 +206,9 @@ class TreeNode { commandResult = this.instance; if (this.instance.needsPromise) { this.needsPromise = true; + // this change was done because the only way function-styled custom commands could be resolved + // is if they contain another NW API command, which could call `node.context.emit('complete')` + // inside `asynctree.js > resolveNode` method for the custom command node. commandResult = this.context; } } else if (this.context instanceof EventEmitter) { // Chai assertions