error handling - Is there a way to add try-catch to every function in Javascript? - Stack Overflow

PHOTO EMBED

Tue Aug 02 2022 14:17:59 GMT+0000 (Coordinated Universal Time)

Saved by @jacopo #javascript

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

https://stackoverflow.com/questions/11742067/is-there-a-way-to-add-try-catch-to-every-function-in-javascript