T
TanStack4mo ago
multiple-amethyst

How to Add a Catch-All Error Handler Middleware in createServerFileRoute?

I'm using createServerFileRoute to define my server routes. I want to implement a catch-all error handler middleware that logs errors to the console and returns a 500 status code for any unhandled errors. In the documentation, I see that middleware can be registered using registerMiddleware, is that only can be used with createServerFn. How can I add a global error handling middleware when using createServerFileRoute?
4 Replies
multiple-amethyst
multiple-amethystOP4mo ago
anyone?
genetic-orange
genetic-orange4mo ago
you can install such a Middleware in the root route
broad-brown
broad-brown4mo ago
how would you add a middleware in the root route? How is that different from specifying a global middleware?
genetic-orange
genetic-orange4mo ago
in __root.tsx:
const middleware = createMiddleware({type: 'request'}).server(({next}) => {return next()})
export const ServerRoute = createServerRootRoute().middleware([middleware])
const middleware = createMiddleware({type: 'request'}).server(({next}) => {return next()})
export const ServerRoute = createServerRootRoute().middleware([middleware])
its a middlware that does not run for server functions, just server routes

Did you find this page helpful?