NeonN
tRPC4y ago
3 replies
Neon

Throw custom TRPCError with specific cause

Hello everyone !
I'm trying to throw a custom TRPCError with a specific cause that i can differentiate from other trpc errors. My use case is to be able to catch error on client and display an error message if the error code is 'BAD_REQUEST' and a specific cause. After investigating i find that something like this should work in my mutation procedure:
throw new TRPCError({
      message: 'Password changed too recently',
      code: 'BAD_REQUEST',
      cause: { name: 'VALIDATION_ERROR', message: "You can only modify your password once a day" },
    });

But in my errorFormatter i only receive this object
{"code":"BAD_REQUEST","name":"TRPCError"}
cause field is not present.

Can someone enlighten me on how to solve this problem ?

Thanks 🙂
Was this page helpful?