Skip to content

Instantly share code, notes, and snippets.

@CrabBot
Created November 14, 2012 22:47
Show Gist options
  • Select an option

  • Save CrabBot/4075399 to your computer and use it in GitHub Desktop.

Select an option

Save CrabBot/4075399 to your computer and use it in GitHub Desktop.
node.js domain fail
var domain = require('domain')
, fs = require('fs')
, trycatch = require('trycatch')
function thirdPartyFoo(callback) {
var d1 = domain.create()
d1.on('error', function(err) {
console.log('1: ', err)
// It's cool, they caught it
callback(err)
// And cleaned it up!
d1.dispose()
})
d1.run(function() {
setTimeout(function() {
// Ooops, they made a mistake
throw new Error('First')
callback()
}, 100)
})
}
function domainFoo(callback) {
var d2 = domain.create()
d2.on('error', function(err) {
console.log('2: ', err)
callback(err)
})
d2.run(function() {
thirdPartyFoo(function(err, res) {
if (err) {
// Oh noes! Better log it.
// It's probably a mongoose error...
// Log the first error in err.errors
console.log(err.errors[0])
// Proceed
callback()
}
})
})
}
function trycatchFoo(callback) {
trycatch(function() {
thirdPartyFoo(function(err, res) {
if (err) {
// Oh noes! Better log it.
// It's probably a mongoose error...
// Log the first error in err.errors
console.log(err.errors[0])
}
// Proceed
callback()
})
}, function(err) {
console.log('3: ', err)
callback(err)
})
}
domainFoo(function(err) {
// Fail. Does not arrive
console.log('=(')
})
/* This works
trycatchFoo(function(err) {
console.log('Success!: ', err.message)
})
*/
// Keep the service alive, so we can tell when it dies
setInterval(function(){},10000)
@Raynos
Copy link
Copy Markdown

Raynos commented Nov 15, 2012

Why does this fail?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment