Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add try-catch to every function in Javascript?

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...

  1. I want to notify the user that the app may not function perfectly
  2. I want to know roughly where the error occurred, so I know where to look for the problem
like image 248
Wytze Avatar asked Jul 31 '12 14:07

Wytze


People also ask

Can I have multiple try catch in a function?

You cannot have multiple try blocks with a single catch block. Each try block must be followed by catch or finally.

Can we use try catch in function?

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.

Can we have multiple catch block in JavaScript?

Unfortunately Javascript does not support multiple catch(error) to allow you to run a different code based on the error type.

Can you have nested try catch in JavaScript?

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.


3 Answers

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);     } } ); 
like image 197
Aaron Digulla Avatar answered Sep 29 '22 01:09

Aaron Digulla


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)
        }
    }
}
like image 45
Fullchee Zhang Avatar answered Sep 29 '22 01:09

Fullchee Zhang


The given answers already work well, just wanted to share a new one using closures.

Define wrapper

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;
    }
}

Your function without a try-catch wrapper

const myFunction = async (x, y) => {
    const sum = x + y;
    if (sum > 10) throw new Error(`sum > 10 custom error`)
    return sum;
}

How to use it

try {
    const wrapperFunction = trycatchWrapper3(myFunction2);
    const output = await wrapperFunction(2, 93)
    console.log(output)
} catch (error) {
    console.error(error)
}

END

like image 24
Jassi Avatar answered Sep 28 '22 23:09

Jassi