Created
December 13, 2012 07:14
-
-
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
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 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(); |
Thanks again @othiym23
ActionHero's cache commands now don't crash the server if something goes wrong :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist needs a mobile view...
Thanks for all your help. It's all starting to make sense now :D