tRPC errorFormatter not inferring types to the frontend #7075
Replies: 1 comment
-
|
The core problem here is that you're setting The Fix: don't set errorFormatter({ shape, error }) {
if (error.cause instanceof ZodError) {
return {
...shape,
message: "Incorrect input format",
data: {
...shape.data,
stack: undefined, // this is fine to remove
validationErrors: error.cause.issues.map((e) => ({
path: e.path.join("."),
message: e.message,
})),
},
};
}
return {
...shape,
data: {
...shape.data,
stack: undefined,
meta:
typeof error.cause === "object" && error.cause !== null
? error.cause
: undefined,
},
};
}Notice I removed the Regarding the type inference issue with your separate repos: when you build and copy the To fix the type inference across repos:
The copy-paste |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My project is in 2 parts
This isn't a monorepo project, rather exists in 2 separate repos
On the backend, I generate the trpc AppRouter types by building the project. I get an index.d.ts file which I copy and paste to the frontend (This is a temporary workaround later I will make this an npm package to share between the 2 repos)
The problem : I am using errorFormatter to send only required fields when error is thrown on the backend but on the frontend, it is not being inferred correctly and I get this error :
TRPCClientError: Unable to transform response from serverThe generated types file (index.d.ts) has the error shape as the DefaultErrorShape not the custom error shape enforced by the errorFormatter.
Here is my errorFormatter
I am also using superjson as the transformer on both server and the client (this might not be the cause of this
Beta Was this translation helpful? Give feedback.
All reactions