I've created a very basic server function for a todo list app (currently stored in a colocated json file) and a very basic middleware, which throws an error if user isn't authenticated, but even though the middleware only has a .server method defined, i'm seeing the error and the error message on the client, and not in the dev server logs
#middleware is throwing an error which isn't visible on server but on client console
8 messages · Page 1 of 1 (latest)
Try using console.log to see if it logs from the client or the server?
i'm getting logs in the server, but shouldn't the thrown error also come in the server? why/how is it going it to the client?
When throwing new Error("Unauthorized") in middleware:
Middleware does not return a normal response.
Instead, TanStack Start catches the error and converts it into an HTTP error response:
Default status code: 500 Internal Server Error (if you haven't set the status beforehand)
Client (React) receives an error response
When you call a server function (for example, via useServerFn, createServerFn, or route loader), the client receives an error response => TanStack Start automatically throws an error in the hook.
Result: you see the error displayed on the client console (and usually in the UI if you have an error boundary or .catch()).
throwing an error in a server function or function middleware will cause this error to be serializer and sent to the client