Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Catching uncaught exceptions with node v0.10.13
// this catches the error (good) but still crashes (bad)
// was process.uncaughtException already deprecated?
// (shouldn't have been yet - http://nodejs.org/api/process.html#process_event_uncaughtexception)
process.on('uncaughtException', function(error) {
return console.error("CAUGHT uncaughtException", error);
});
throw new Error("A big error!");
setInterval((function() {
return console.log('still here');
}), 1000);
# this doesn't even catch the error
## apparently as designed -- see https://github.com/jashkenas/coffee-script/issues/1438
process.on 'uncaughtException', (error)->
console.error "CAUGHT uncaughtException", error
throw new Error "A big error!"
setInterval (-> console.log('still here')), 1000
// this catches and keeps running.
var domain = require('domain');
var myDomain = domain.create()
myDomain.on('error', function(error){
console.error("Caught domain error", error);
});
myDomain.run(function(){
setInterval((function() {
return console.log('still here');
}), 1000);
throw new Error("An error in the domain!");
});
## this fails!! - normal exception & crash
## (run w/ `coffee [PATH]`)
domain = require 'domain'
myDomain = domain.create()
myDomain.on 'error', (error)->
console.error "Caught domain error", error
myDomain.run ->
setInterval (-> return console.log 'still here'), 1000
throw new Error "An error in the domain!"
## interestingly, this works! just added `process.nextTick`
## (ideas from https://groups.google.com/forum/#!msg/coffeescript/TzQyqVyn1JQ/FtXnMgwKE80J)
domain = require 'domain'
myDomain = domain.create()
myDomain.on 'error', (error)->
console.error "Caught domain error", error
myDomain.run ->
process.nextTick ->
setInterval (-> return console.log 'still here'), 1000
throw new Error "An error in the domain!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment