Last active
December 22, 2015 11:19
-
-
Save benbuckman/6465040 to your computer and use it in GitHub Desktop.
Catching uncaught exceptions with node v0.10.13
This file contains hidden or 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
// 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 file contains hidden or 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
# 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 file contains hidden or 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
// 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 file contains hidden or 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
## 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!" |
This file contains hidden or 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
## 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