For error reporting, I would like to insert a try-catch wrapper around the code of every function I have.
So basically I want to replace
function foo(arg){
bar();
}
...with...
function foo(arg){
try {
bar()
}
catch(e){
customErrorHandler(e)
}
}
Is there a way to apply this generic try-catch thing to all functions without manually editing all of them? For example by modifying the prototype of the Function object?
EDIT
Why I want to try-catch all my functions: I am building an HTML5 app that I'm publishing on iOS and Android. I can tell from my current rudimentary javascript error reporting that even though the app runs nicely on my own device, errors do occur on some other devices.
My objective is twofold: whenever a javascript error occurs on someone's device...
You cannot have multiple try blocks with a single catch block. Each try block must be followed by catch or finally.
The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
Unfortunately Javascript does not support multiple catch(error) to allow you to run a different code based on the error type.
You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.
This isn't simple since there is no way to find all JavaScript function defined everywhere. For example, any such approach would probably miss callback functions which are defined at runtime.
You also probably don't want to wrap all functions because that would include browser functions and functions from JavaScript libraries that you certainly don't want to wrap.
A much better approach is probably to define a function which wraps another function:
var tcWrapper = function(f) { return function() { try { f.apply(this, arguments); } catch(e) { customErrorHandler(e) } } }
Now you can use this function to decorate anything that you want. Wrapping will become more simple if you use name spaces:
var NS = { f: function() { } }
Just put all functions to wrap in a special namespace and then iterate over the namespace:
$.each( NS, function(i,n) { var p = NS[i]; if( typeof p === 'function' ) { NS[i] = tcWrapper(p); } } );
I don't have enough reputation to comment on the accepted answer.
I added a return
before the f.apply
to pass up the return value as well.
var tcWrapper = function(f) {
return function() {
try {
return f.apply(this, arguments);
} catch(e) {
customErrorHandler(e)
}
}
}
The given answers already work well, just wanted to share a new one using closures.
const tryCatchWrapper = (executable) => async (...args) => {
try {
const result = await executable(...args);
return result;
} catch (error) {
// use any custom handler here
error.message = `[${executable.name}] - ${error.message}`;
error.data = {...error.data, input_args: args}
throw error;
}
}
const myFunction = async (x, y) => {
const sum = x + y;
if (sum > 10) throw new Error(`sum > 10 custom error`)
return sum;
}
try {
const wrapperFunction = trycatchWrapper3(myFunction2);
const output = await wrapperFunction(2, 93)
console.log(output)
} catch (error) {
console.error(error)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With