Skip to content

Instantly share code, notes, and snippets.

@othiym23
Forked from CrabBot/gist:4075399
Created November 14, 2012 23:23
Show Gist options
  • Save othiym23/4075562 to your computer and use it in GitHub Desktop.
Save othiym23/4075562 to your computer and use it in GitHub Desktop.
node.js domain fail
'use strict';
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(err.errors[0]);
}
});
});
}
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(err.errors[0]);
});
}, function (err) {
console.log('3: ', err);
callback(err);
});
}
domainFoo(function (err) {
// Fail. Does not arrive
console.log('=(: ', err);
});
/* This works */
// trycatchFoo(function(err) {
// console.log('Success!: ', err.message);
// });
// Keep the service alive, so we can tell when it dies
setInterval(function(){},10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment