Last active
October 12, 2015 18:04
-
-
Save tjmehta/9c4d4266b93c71465ea0 to your computer and use it in GitHub Desktop.
Unexpected domain error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Domain = require('domain') | |
var d1 = Domain.create(); | |
var emitter; | |
d1.on('error', handleCreateErr) | |
d1.run(function () { | |
emitter = createEmitter() | |
}) | |
var d2 = Domain.create(); | |
d2.on('error', handleEmitterErr) | |
d2.run(function () { | |
emitter.on('foo', function () { | |
throw new Error('something bad happenned'); | |
}) | |
emitter.emit('foo') | |
}) | |
function handleCreateErr (err) { | |
console.error('error creating event emitter', err.stack); | |
} | |
function handleEmitterErr (err) { | |
console.error('error using event emitter', err.stack); | |
} |
An error occurs above when using the event emitter, but ends up triggering an error
event for d1
.
This leads to unexpected behavior, and print the wrong error message (error creating event emitter).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If an event emitter is passed around through an application, I think it is a bit weird for it to still emit it's errors to the domain it was created in.