Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Created October 20, 2010 12:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominictarr/636290 to your computer and use it in GitHub Desktop.
Save dominictarr/636290 to your computer and use it in GitHub Desktop.
ways to process errors in node js
//test-uncaught-errors
var sys = require('sys')
, EventEmitter = require('events').EventEmitter;
function error1 (err){
sys.puts("Handle: " + err);
}
function error2 (err){
sys.puts("JANDAL: " + err);
}
function exit (){
sys.puts("exiting ");
}
process.on('exit',exit);
process.on('uncaughtException',error1);
process.on('uncaughtException',error2);
emitter = new EventEmitter();
function passError(err){
sys.puts("passing error:" + err);
throw new Error("PASSED:" + err);
}
emitter.on('error',passError);
function emitErr(message){
process.nextTick(function(){
emitter.emit('error',Error(message));
});
}
function throwErr(message){
process.nextTick(function(){
throw new Error(message);
});
}
emitErr("too many parenthesis");
emitErr("unexpected kEND");
process.nextTick(function(){
sys.puts(' thrown errors\n');
});
throwErr("bad magic number");
throwErr("");
function safeError (err){
sys.puts("saved: " + err);
}
emitter2 = new EventEmitter();
emitter2.on('error',safeError);
emitter2.emit('error',new Error("2nd last: directly emmited 1"));
emitter2.emit('error',new Error("last: directly emmited 2"));
sys.puts(' the nextTick errors won\'t occur\n'
+ ' until node gets back to the event loop\n'
+ ' thats why the last errors are first\n')
//if there is no listeners for an error even, node will throw it.
emitter3 = new EventEmitter();
emitter3.emit('error',new Error("catch this"));
//in this case it will still be intercepted by
//process.on('uncaughtException')
// by error1 and error2.
@keywhether
Copy link

Doodle Cricket is an appealing and highly fun online cricket game based on the renowned Google Doodle cricket theme.

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