Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


I think this is about browser crashes, not browsers throwing errors in user scripts.


Right. I should have put it under the Sentry discussion.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: