2 ms·
It's a poor man's version, and it doesn't catch all errors, but it works in all browsers export function wrapInErrorHandler<Func extends (...args: any[]) =
by tgv 4y ago
It's a poor man's version, and it doesn't catch all errors, but it works in all browsers
export function wrapInErrorHandler<Func extends (...args: any[]) => any>(func: Func): (...args: InferArguments<Func>) => InferReturn<Func> {
return function (...args: InferArguments<Func>) {
try {
return func.apply(null, args);
} catch (err: any) {
if (err instanceof LoggedError) {
throw err;
}
post({ your- error - object});
throw new LoggedError(err);
}
};
}
I wrap (some) event handlers and callbacks in this function (wrapInErrorHandler(f) returns a function of the same type as f). If they throw an error, the data is posted to a simple json collecting endpoint, which triggers an email action (not too frequently). LoggedError is wrapper for err, to avoid logging the same error multiple times.