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(ar
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);
}
} );