Reputation: 956
I created a command line app in node.js which heavily relies on file system module of node (read, update, create new file etc). I write many functions for the same purpose.
Now my question is : 'Can I globally catch the error that occurred due to any function?'
For example:
func1(); // throws some error: Cannot find file , errCode:xyz
func2(); // throws some error: Cannot find file , errCode:xyz
//some global error catcher
func(catch errCode:xyz){
console.log("Cannot find file. Make sure file exist and path is correct.");
}
ss
Upvotes: 0
Views: 93
Reputation: 2630
You can listen to https://nodejs.org/api/process.html#process_event_uncaughtexception
process.on('uncaughtException', function handler(err) {
// do stuf based on err, but make sure node.js quits after
// displaying the error and performing basic cleanup - see below
})
However this is not recommended, and you certainly should not let your application keep running after this since it might be in an unknown state.
You functions should perform a callback with the error first, node-style, and the function that passed the callback should handle the error in the correct way.
Upvotes: 2