v28V
tRPC3d ago
v28

trpc error formatting

Hello,

I am wondering if it is possible to properly type my "cause.code"? I am hoping to create custom error codes so my frontend can decide which type of UI to display to the user. But, ideally this would be in a typesafe way so im not guessing what the error codes are. Is there a way i can tell trpc this, without having to use as CustomErrorCodes in every instance I want to use it in?

client:
    const { error: invalidCaptcha } = await tryCatch(
        api.verification.isCaptchaValid({ guildId, uuid }),
    );

    console.log(invalidCaptcha?.cause?.code); // Property code does not exist on type {}.

server:
            if (!verificationCache) {
                throw new TRPCError({
                    code: "BAD_REQUEST",
                    message:
                        "This captcha has expired.",
                    cause: {
                        code: "CAPTCHA_EXPIRED",
                    },
                });
            }
Was this page helpful?