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

后端 未结 5 1230
心在旅途
心在旅途 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 03:07

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

提交回复
热议问题