tRPCttRPC
Powered by
NeonN
tRPC•4y 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" },
    });
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"}
{"code":"BAD_REQUEST","name":"TRPCError"}
cause field is not present.

Can someone enlighten me on how to solve this problem ?

Thanks 🙂
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
difference between cause and message on TRPCError?
NickNNick / ❓-help
3y ago
throw custom error
player1Pplayer1 / ❓-help
2y ago
TRPCError class, public readonly cause?: Error; this line should have override function
fahadFfahad / ❓-help
3y ago