I’ve looked through the threads here on this subject and can’t find what I want.
I have a bunch of functions that return string and I want them to not return a string if the input is bad and I don’t want the program to exit either AND I don’t want to wrap every call in try / catch.
function foo(num){
if(num > 5){
throw SomeException("Input too big")
}else{
return "bar"+num
}
}
I want to log an error like “6 is too big for foo”, instead of exiting the program. But this will be kind of an api, so I don’t want the user to have to try/catch every time they use
one of these functions. This is a nodejs script.
this works, but is a awkward:
f = {
command : function(cmd,arg){
try {
console.log(q[cmd](arg));
}
catch (e) {
console.log("Error: " + e + " for " + cmd);
}
},
foo : function(str){
throw ("foo error");
},
foo2 : function(str){
throw ("foo2 error");
}
};
f.command("foo",2);
f.command("foo2",3);
[[Edit]] It’s a nodeJS script. Then make the API asynchronous. We do not do synchronous operations in nodeJS. Please conserve the event loop.
You can also rework your API to be asynchronous
You can reference a public logger instance.
MyLibrary.errorHandler = defaultErrorHandler() || config.errorHandlerThen have the defaultErrorHandler be