T
TanStack4w ago
fascinating-indigo

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
fascinating-indigo
fascinating-indigoOP3w ago
anyone?
diverse-rose
diverse-rose2w ago
you can install such a Middleware in the root route
inland-turquoise
inland-turquoise2w ago
how would you add a middleware in the root route? How is that different from specifying a global middleware?
diverse-rose
diverse-rose2w 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?