-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
domain: fix error emit handling #17588
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,32 +399,35 @@ EventEmitter.init = function() { | |
| const eventEmit = EventEmitter.prototype.emit; | ||
| EventEmitter.prototype.emit = function emit(...args) { | ||
| const domain = this.domain; | ||
| if (domain === null || domain === undefined || this === process) { | ||
| return Reflect.apply(eventEmit, this, args); | ||
| } | ||
|
|
||
| const type = args[0]; | ||
| // edge case: if there is a domain and an existing non error object is given, | ||
| // it should not be errorized | ||
| // see test/parallel/test-event-emitter-no-error-provided-to-error-event.js | ||
| if (type === 'error' && args.length > 1 && args[1] && | ||
| !(args[1] instanceof Error)) { | ||
| domain.emit('error', args[1]); | ||
| return false; | ||
| } | ||
| const shouldEmitError = type === 'error' && | ||
| this.listenerCount(type) > 0; | ||
|
|
||
| domain.enter(); | ||
| try { | ||
| // Just call original `emit` if current EE instance has `error` | ||
| // handler, there's no active domain or this is process | ||
| if (shouldEmitError || domain === null || domain === undefined || | ||
| this === process) { | ||
| return Reflect.apply(eventEmit, this, args); | ||
| } catch (er) { | ||
| } | ||
|
|
||
| if (type === 'error') { | ||
| const er = args.length > 1 && args[1] ? | ||
| args[1] : new errors.Error('ERR_UNHANDLED_ERROR'); | ||
|
|
||
| if (typeof er === 'object' && er !== null) { | ||
|
||
| er.domainEmitter = this; | ||
| er.domain = domain; | ||
| er.domainThrown = false; | ||
| } | ||
|
|
||
| domain.emit('error', er); | ||
| return false; | ||
| } finally { | ||
| domain.exit(); | ||
| } | ||
|
|
||
| domain.enter(); | ||
| const ret = Reflect.apply(eventEmit, this, args); | ||
| domain.exit(); | ||
|
|
||
| return ret; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const domain = require('domain').create(); | ||
| const EventEmitter = require('events'); | ||
|
|
||
| domain.on('error', common.mustNotCall()); | ||
|
|
||
| const ee = new EventEmitter(); | ||
|
|
||
| const plainObject = { justAn: 'object' }; | ||
| ee.once('error', common.mustCall((err) => { | ||
| assert.deepStrictEqual(err, plainObject); | ||
| })); | ||
| ee.emit('error', plainObject); | ||
|
|
||
| const err = new Error('test error'); | ||
| ee.once('error', common.expectsError(err)); | ||
| ee.emit('error', err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args[1] || new errors.Error('ERR_UNHANDLED_ERROR')should be enough. But I think it would be better to stay grammatically closer to the old implementation.Something like:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That triggers a slow path in V8. We need the
ifcheck or a ternary. This version does the same in less lines and we don't needlazyErrors, which is the main reason we didn't just use a ternary before.