Reputation: 1295
I am quite new to node.js but I totally love it. However, I am strugling to understand proper error handling.
Over web there are a lot of good resources about that but they mostly are old and refer to callback-based programing. I prefer to use promises. With promises it is even more easier to handle errors as you dont have to duplicate code for every operational error that could happen - instead you just catch it once and handle (or propagate to caller).
However in node it is important to distinguish between error type and perform gracefull crash and restart of application if programmatic error has happened. Consider this callback-based code example:|
function myFunc (input, cb){
if(typeof input === 'undefined'){
throw new Error('Invalid argument');
}
asinc1(input, function(e, data){
if(err) return cb(e, null);
asinc2(data, function(e, result){
if(err) return cb(e, null);
return cb(null, result);
});
});
}
/* USAGE */
try{
myFunc(input, function(e, result){
if(err) // handle operational error.
else{
//continue what ever you need with result
}
})
}catch(e){
//crash gracefully
}
However if we write this code in promise-based approach:
function myFunc (input){
if(typeof input === 'undefined'){
throw new Error('Invalid argument');
}
return asinc1(input)
.then(asinc2);
}
/* USAGE */
myFunc(input)
.then(function(result){
//continue what ever you need with result
}).catch(function(e){
//handle both - programmatic and operational errors
})
there is no way to distinguish between error type thus I dont know what exactly to do if error happens.
Upvotes: 2
Views: 1312
Reputation: 1295
I have been doing some more research and found two possible solutions. However I dont feel 100% sure about them so would still love to hear other answers or someone to comment on my approach.
There are two options:
To implement first one I have found two good ways. One is to create a custom MyError object, second is simply adding additional flag:
if(programmatic error){
let e = new Error('Message');
e.isProgrammtic = true;
throw e;
}
Now you can catch it and pass to global logger object where do a simple check if it is a programatic error and act accordingly.
Second option is possible using promise library like bluebird. They have .error method that catches explicity operational errors. So in my example we could do something like this:
myFunc(input)
.then(function(result){
//continue what ever you need with result
}).catch(function(e){
//handle programatic errors. Crash gracefully and restart
}).error(function(e){
// handle operational error. Dont crash
})
Upvotes: 2
Reputation: 1
You can return
Promise.reject()
instead of throw
ing Error
function asinc1() {}
function asinc2() {}
function myFunc(input) {
if (typeof input === "undefined") {
return Promise.reject(new Error('Invalid argument'));
}
return asinc1(input)
.then(asinc2);
}
/* USAGE */
myFunc(void 0)
.then(function(result) {
//continue what ever you need with result
}).catch(function(e) {
//handle both - programmatic and operational errors
console.log(e)
})
Upvotes: -1