Catch TRPCError, ZoddError on the front-end

CCaptain12/10/2022
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
        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.
CCaptain12/10/2022
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
        }
CCaptain12/10/2022
the console.log inside the if statement doesnt get printed
A/Kalex / KATT12/10/2022
Add the actual zod error as the cause in the trpc error instead
A/Kalex / KATT12/10/2022
Then use error.cause
CCaptain12/10/2022
errors.cause is undefined when on client
A/Kalex / KATT12/10/2022
On the client? If you want to propagate to the client you need error formatting
A/Kalex / KATT12/10/2022
And it'll be a TRPCClientError
CCaptain12/11/2022
to want to catch the zod parse error, you actually have to throw a zodError
CCaptain12/11/2022
should be included in the docs in my opinion
CCaptain12/11/2022
also dont flatten the error when formatting
CCaptain12/11/2022
also dont safparse
A/Kalex / KATT12/11/2022
feel free to improve the docs! 🙂 would love some help!

none of the people working on trpc does it as a full-time job
Mmsalsbery12/11/2022
Good to know! (The first part I mean, I knew the second part)