Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active June 7, 2021 16:00
Show Gist options
  • Save balupton/5560110 to your computer and use it in GitHub Desktop.
Save balupton/5560110 to your computer and use it in GitHub Desktop.
Node.js Best Practice Exception Handling
var divide = function(x,y,next) {
// if error condition?
if ( y === 0 ) {
// "throw" the error safely by calling the completion callback
// with the first argument being the error
var err = new Error("Can't divide by zero");
next(err);
}
else {
// no error occured, continue on
next(null, x/y);
}
};
divide(4,2,function(err,result){
// did an error occur?
if ( err ) {
// handle the error safely
console.log('4/2=err', err);
}
else {
// no error occured, continue on
console.log('4/2='+result);
}
});
divide(4,0,function(err,result){
// did an error occur?
if ( err ) {
// handle the error safely
console.log('4/0=err', err);
}
else {
// no error occured, continue on
console.log('4/0='+result);
}
});
var d = require('domain').create();
d.on('error', function(err){
// handle the error safely
console.log(err);
});
// catch the uncaught errors in this asynchronous or synchronous code block
d.run(function(){
// the asynchronous or synchronous code that we want to catch thrown errors on
var err = new Error('example');
throw err;
});
// Definite our Divider Event Emitter
var events = require('events');
var Divider = function(){
events.EventEmitter.call(this);
}; require('util').inherits(Divider, events.EventEmitter);
// Add the divide function
Divider.prototype.divide = function(x,y){
// if error condition?
if ( y === 0 ) {
// "throw" the error safely by emitting it
var err = new Error("Can't divide by zero");
this.emit('error', err);
}
else {
// no error occured, continue on
this.emit('divided', x, y, x/y);
}
// Chain
return this;
};
// Create our divider and listen for errors
var divider = new Divider();
divider.on('error', function(err){
// handle the error safely
console.log(err);
});
divider.on('divided', function(x,y,result){
console.log(x+'/'+y+'='+result);
});
// Divide
divider.divide(4,2).divide(4,0);
/* Outputs:
4/2=2
[Error: Can't divide by zero]
*/
// Define divider as a syncrhonous function
var divideSync = function(x,y) {
// Prepare
var result;
// if error condition?
if ( y === 0 ) {
// "throw" the error safely by returning it
result = new Error("Can't divide by zero");
}
else {
// no error occured, continue on
result = x/y;
}
// Return
return result;
};
// Divide 4/2
var result;
result = divideSync(4,2);
// did an error occur?
if ( result instanceof Error ) {
// handle the error safely
console.log('4/2=err', result);
}
else {
// no error occured, continue on
console.log('4/2='+result);
}
// Divide 4/0
result = divideSync(4,0);
// did an error occur?
if ( result instanceof Error ) {
// handle the error safely
console.log('4/0=err', result);
}
else {
// no error occured, continue on
console.log('4/0='+result);
}
// catch the uncaught errors in this synchronous code block
// try catch statements only work on synchronous code
try {
// the synchronous code that we want to catch thrown errors on
var err = new Error('example');
throw err;
} catch (err) {
// handle the error safely
console.log(err);
}
// catch the uncaught errors that weren't wrapped in a domain or try catch statement
// do not use this in modules, but only in applications, as otherwise we could have multiple of these bound
process.on('uncaughtException', function(err) {
// handle the error safely
console.log(err);
});
// the asynchronous or synchronous code that we want to catch thrown errors on
var err = new Error('example');
throw err;
@antonlvovych
Copy link

Cool, thx!
But what about browsers? What are the best practice to handle error in the browser?

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