TanStackT
TanStackโ€ข4mo agoโ€ข
9 replies
urgent-maroon

Catch error in a global middleware

hii
i would like to send any unhandled error to sentry. to do that, i wrote the following code
const sentryMiddleware = createMiddleware().server(async ({ request, next }) => {
    try {
        return await next();
    }
    catch (error) {
        Sentry.captureException(error, {
            user: getUser(request)
        });
        throw error;
    }
});

export const startInstance = createStart(() => {
    return {
        requestMiddleware: [sentryMiddleware]
    };
});


unfortunately, in the case of server functions, the error seems to be handled by this code before my middleware
https://github.com/TanStack/router/blob/80a9f3dcdf70bb055821c7ab14117b8d77027e3f/packages/start-server-core/src/server-functions-handler.ts#L284

also, for some reason, the error message is sent to the frontend even when running in prod

can i somehow setup my error handler to work before/instead of server-functions-handler? ๐Ÿ‘€
Was this page helpful?