| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 28edc1d commit 35471bc
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -399,32 +399,35 @@ EventEmitter.init = function() { | |||
| 399 | 399 | const eventEmit = EventEmitter.prototype.emit; | |
| 400 | 400 | EventEmitter.prototype.emit = function emit(...args) { | |
| 401 | 401 | const domain = this.domain; | |
| 402 | - if (domain === null || domain === undefined || this === process) { | ||
| 403 | - return Reflect.apply(eventEmit, this, args); | ||
| 404 | - } | ||
| 405 | 402 | ||
| 406 | 403 | const type = args[0]; | |
| 407 | - // edge case: if there is a domain and an existing non error object is given, | ||
| 408 | - // it should not be errorized | ||
| 409 | - // see test/parallel/test-event-emitter-no-error-provided-to-error-event.js | ||
| 410 | - if (type === 'error' && args.length > 1 && args[1] && | ||
| 411 | - !(args[1] instanceof Error)) { | ||
| 412 | - domain.emit('error', args[1]); | ||
| 413 | - return false; | ||
| 414 | - } | ||
| 404 | + const shouldEmitError = type === 'error' && | ||
| 405 | + this.listenerCount(type) > 0; | ||
| 415 | 406 | ||
| 416 | - domain.enter(); | ||
| 417 | - try { | ||
| 407 | + // Just call original `emit` if current EE instance has `error` | ||
| 408 | + // handler, there's no active domain or this is process | ||
| 409 | + if (shouldEmitError || domain === null || domain === undefined || | ||
| 410 | + this === process) { | ||
| 418 | 411 | return Reflect.apply(eventEmit, this, args); | |
| 419 | - } catch (er) { | ||
| 420 | - if (typeof er === 'object' && er !== null) { | ||
| 412 | + } | ||
| 413 | + | ||
| 414 | + if (type === 'error') { | ||
| 415 | + const er = args.length > 1 && args[1] ? | ||
| 416 | + args[1] : new errors.Error('ERR_UNHANDLED_ERROR'); | ||
| 417 | + | ||
| 418 | + if (typeof er === 'object') { | ||
| 421 | 419 | er.domainEmitter = this; | |
| 422 | 420 | er.domain = domain; | |
| 423 | 421 | er.domainThrown = false; | |
| 424 | 422 | } | |
| 423 | + | ||
| 425 | 424 | domain.emit('error', er); | |
| 426 | 425 | return false; | |
| 427 | - } finally { | ||
| 428 | - domain.exit(); | ||
| 429 | 426 | } | |
| 427 | + | ||
| 428 | + domain.enter(); | ||
| 429 | + const ret = Reflect.apply(eventEmit, this, args); | ||
| 430 | + domain.exit(); | ||
| 431 | + | ||
| 432 | + return ret; | ||
| 430 | 433 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const domain = require('domain').create(); | ||
| 6 | + const EventEmitter = require('events'); | ||
| 7 | + | ||
| 8 | + domain.on('error', common.mustNotCall()); | ||
| 9 | + | ||
| 10 | + const ee = new EventEmitter(); | ||
| 11 | + | ||
| 12 | + const plainObject = { justAn: 'object' }; | ||
| 13 | + ee.once('error', common.mustCall((err) => { | ||
| 14 | + assert.deepStrictEqual(err, plainObject); | ||
| 15 | + })); | ||
| 16 | + ee.emit('error', plainObject); | ||
| 17 | + | ||
| 18 | + const err = new Error('test error'); | ||
| 19 | + ee.once('error', common.expectsError(err)); | ||
| 20 | + ee.emit('error', err); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments