diff options
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/index.ts | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/packages/trpc/index.ts b/packages/trpc/index.ts index bcfa7211..51c713c0 100644 --- a/packages/trpc/index.ts +++ b/packages/trpc/index.ts @@ -2,6 +2,7 @@ import { db } from "@hoarder/db"; import serverConfig from "@hoarder/shared/config"; import { TRPCError, initTRPC } from "@trpc/server"; import superjson from "superjson"; +import { ZodError } from "zod"; type User = { id: string; @@ -21,6 +22,19 @@ export type Context = { // is common in i18n libraries. const t = initTRPC.context<Context>().create({ transformer: superjson, + errorFormatter(opts) { + const { shape, error } = opts; + return { + ...shape, + data: { + ...shape.data, + zodError: + error.code === 'BAD_REQUEST' && error.cause instanceof ZodError + ? error.cause.flatten() + : null, + }, + }; + }, }); export const createCallerFactory = t.createCallerFactory; // Base router and procedure helpers |
