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

后端 未结 5 1249
心在旅途
心在旅途 2021-02-04 02:32

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         


        
5条回答
  •  遇见更好的自我
    2021-02-04 02:56

    I wonder (this is pure speculation, so not sure if this would work) you could do something like this:

    function callTryCatch(functionSignature) {
        try {
            eval(functionSignature);
        } catch (e) {
            customErrorHandler(e);
        }
    }
    
    function entryPoint() {
        callTryCatch(function() {
            // do function logic
        });
    }
    

    Again, this is pure speculation and I haven't tested but if it's even possible I think the key lies in the eval statement.

提交回复
热议问题