Aezral
Aezral15mo ago

What's the type of errorFormatter parameter

I wanna know how could i get the type of the errorFormatter parameter, so i can move the errorFormatter logic to another file. Environment: Node v18.15.0
2 Replies
Nick
Nick15mo ago
Right click and go to type definition then start copying things out, if any types you need aren’t already exported then open a GitHub issue and we can do it if it’s sensible
NeoBean
NeoBean12mo ago
For others looking for the type, i believe it is ErrorFormatter<Context, DefaultErrorShape>
// `Context` is basically `inferAsyncReturnType<typeof createContext>`
export const errorFormatter: ErrorFormatter<Context, DefaultErrorShape> = opts => {
/** do your things, maybe replace DefaultErrorShape */
return opts.shape
}
// `Context` is basically `inferAsyncReturnType<typeof createContext>`
export const errorFormatter: ErrorFormatter<Context, DefaultErrorShape> = opts => {
/** do your things, maybe replace DefaultErrorShape */
return opts.shape
}
Error related types can be found in @trpc/server/dist/error/formatter I think this would be the way to extend the default error shape
import { DefaultErrorData, ErrorFormatter } from '@trpc/server/dist/error/formatter'
import { TRPCErrorShape } from '@trpc/server/dist/rpc'
import { TRPC_ERROR_CODE_NUMBER } from '@trpc/server/http'
import { typeToFlattenedError, ZodError } from 'zod'
import { Context } from './trpcCore'

interface CustomErrorShape
extends TRPCErrorShape<
TRPC_ERROR_CODE_NUMBER,
/** eslint-disable-next-line @typescript-eslint/no-explicit-any */
DefaultErrorData & { zodError: typeToFlattenedError<any, string> | null }
> {
message: string
code: TRPC_ERROR_CODE_NUMBER
}
import { DefaultErrorData, ErrorFormatter } from '@trpc/server/dist/error/formatter'
import { TRPCErrorShape } from '@trpc/server/dist/rpc'
import { TRPC_ERROR_CODE_NUMBER } from '@trpc/server/http'
import { typeToFlattenedError, ZodError } from 'zod'
import { Context } from './trpcCore'

interface CustomErrorShape
extends TRPCErrorShape<
TRPC_ERROR_CODE_NUMBER,
/** eslint-disable-next-line @typescript-eslint/no-explicit-any */
DefaultErrorData & { zodError: typeToFlattenedError<any, string> | null }
> {
message: string
code: TRPC_ERROR_CODE_NUMBER
}
More Posts