tRPCttRPC
Powered by
BumblebeeB
tRPC•14mo ago•
2 replies
Bumblebee

Client receives 200 status when throwing TRPCError

Using pnpm, Next 14 app router and tRPC v11 mainly based on create-t3-turbo.

If I have a query on the client and then throw, for example, a TRPCError with code UNAUTHORIZED, the client is getting back a 200 rather than a 401.
const handler = async (req: Request) => {
  ...
  const response = await fetchRequestHandler({
    endpoint: "/api/trpc",
    router: appRouter,
    req,
    createContext: () =>
      createTRPCContext({
        ...
      }),
    onError({ error, path }) {
      console.error(`>>> tRPC Error on '${path}'`, error);
    },
  });

  setCorsHeaders(response);
  return response;
};
const handler = async (req: Request) => {
  ...
  const response = await fetchRequestHandler({
    endpoint: "/api/trpc",
    router: appRouter,
    req,
    createContext: () =>
      createTRPCContext({
        ...
      }),
    onError({ error, path }) {
      console.error(`>>> tRPC Error on '${path}'`, error);
    },
  });

  setCorsHeaders(response);
  return response;
};

The onError callback is properly getting and logging the 401 but the response sent to the client is a 200. Is there any way to propagate the error to the client?

The same behaviour can be seen on a fresh create-t3-app if a repro is wanted
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

TRPCError that has TRPCError as cause
TkDodo 🔮TTkDodo 🔮 / ❓-help
3y ago
when throwing a TRPCError, is there a way to include an internal error code ?
Ahmed EidAAhmed Eid / ❓-help
4y ago
Missing keys in TRPCError when returning as JSON
molochMmoloch / ❓-help
3y ago
Customizing the TRPCError type
Emmie PäivärintaEEmmie Päivärinta / ❓-help
6mo ago