T
Join ServertRPC
❓-help
Catch TRPCError, ZoddError on the front-end
i am throwing a TRPCError in a mutation. i dont understand how to catch this error in the OnError method. Please help me with this.
i have the following:
ServerSide
ClientSide
everyting is undefined except for message. but i'd like to include ZodError to the cause. doesn't work either.
browser console.
i have the following:
ServerSide
throw new TRPCError({
message: 'Password changed too recently',
code: 'BAD_REQUEST',
cause: Error("You can only modify your password once a day", { cause: 'VALIDATION_ERROR' }),
});
ClientSide
const { code, message, name, cause, stack } = error as TRPCError
console.log(code, message, name, cause, stack)
everyting is undefined except for message. but i'd like to include ZodError to the cause. doesn't work either.
browser console.
const error: TRPCError = {
name: "TRPCError",
code: "BAD_REQUEST",
message: "\"password\" must be at least 4 characters"
}
if (error instanceof TRPCError) {
const httpCode = getHTTPStatusCodeFromError(error);
console.log(httpCode); // 400
}
the console.log inside the if statement doesnt get printed
Add the actual zod error as the cause in the trpc error instead
Then use error.cause
errors.cause is undefined when on client
On the client? If you want to propagate to the client you need error formatting
And it'll be a TRPCClientError
to want to catch the zod parse error, you actually have to throw a zodError
should be included in the docs in my opinion
also dont flatten the error when formatting
also dont safparse
feel free to improve the docs! 🙂 would love some help!
none of the people working on trpc does it as a full-time job
none of the people working on trpc does it as a full-time job
Good to know! (The first part I mean, I knew the second part)