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 unhandledRejection on error inside function-based custom commands. #4309

Merged
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
11 changes: 10 additions & 1 deletion lib/api/_loaders/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/core/treenode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down