Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created December 13, 2012 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantahler/4274698 to your computer and use it in GitHub Desktop.
Save evantahler/4274698 to your computer and use it in GitHub Desktop.
Help with node.js' handling of domain exceptions from clients created outside of their scopes. Check the comment for description
var domain = require('domain');
var redis = require('redis');
var eventEmitter = require('events').EventEmitter;
var tests = [];
var testCounter = 0;
var runTest = function(){
if(tests.length > testCounter){
tests[testCounter]();
}else{
console.log("all done!")
process.exit();
}
}
var myDomain = new domain.create();
myDomain.on('error', function(err){
console.log('Yo, I just saved you from the error: ' + err);
testCounter++;
runTest();
});
// PASSING
tests[0] = function(){
myDomain.run(function(){
throw new Error('A simple error');
});
};
// PASSING
tests[1] = function(){
myDomain.run(function(){
setTimeout(function(){
process.nextTick(function(){
var E = new eventEmitter;
E.on('thing', function(){
throw new Error('A deeply nested error');
})
setTimeout(function(){
E.emit('thing');
}, 100)
});
}, 100);
});
}
// PASSING
var Emm = new eventEmitter;
Emm.on('thing', function(){
throw new Error('Emmited Error defined outside of scope');
})
tests[2] = function(){
myDomain.run(function(){
setTimeout(function(){
Emm.emit('thing');
}, 100)
})
};
// PASSING
tests[3] = function(){
myDomain.run(function(){
clientA = redis.createClient();
clientA.hget('hash', 'key', function(err, data){
throw new Error('An error after redis (A)');
});
});
}
// PASSING
tests[4] = function(){
clientB = redis.createClient();
myDomain.run(function(){
clientB.hget('hash', 'key', function(err, data){
throw new Error('An error after redis (B)');
});
});
};
// FAILING
clientC = redis.createClient();
tests[5] = function(){
myDomain.run(function(){
clientC.hget('hash', 'key', function(err, data){
throw new Error('An error after redis (C)');
});
});
};
// start it up
runTest();
@evantahler
Copy link
Author

Thanks again @othiym23

ActionHero's cache commands now don't crash the server if something goes wrong :D

evantahler/actionhero@c5ebfc0

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