Skip to content

custom errorFormatter doesn't work for me #5555

Closed Answered by KATT
quxiaodong asked this question in Q&A
Discussion options

You must be logged in to vote

Follow the docs. You need to spread the incoming properties the same way the docs

https://trpc.io/docs/server/error-formatting

Eg

import { initTRPC } from '@trpc/server';
export const t = initTRPC.context<Context>().create({
  errorFormatter(opts) {
    const { shape, error } = opts;
    return {
      ...shape,
      data: {
        ...shape.data,
        zodError:
          error.code === 'BAD_REQUEST' && error.cause instanceof ZodError
            ? error.cause.flatten()
            : null,
      },
    };
  },
});

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by KATT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants