tRPCttRPC
Powered by
v28V
tRPC•2mo ago•
4 replies
v28

trpc error formatting

@trpc/server@trpc/next@trpc/client@trpc/react
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
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 {}.
    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",
                    },
                });
            }
            if (!verificationCache) {
                throw new TRPCError({
                    code: "BAD_REQUEST",
                    message:
                        "This captcha has expired.",
                    cause: {
                        code: "CAPTCHA_EXPIRED",
                    },
                });
            }
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

Zod formatting Error
Nacho EliasNNacho Elias / ❓-help
2y ago
Error Handling vs Error Formatting
rdRrd / ❓-help
3y ago
trpc error
junior1Jjunior1 / ❓-help
3y ago
Error formatting with trpcExpress.createExpressMiddleware
elisEelis / ❓-help
2y ago