T
tRPC

❓-help

Error Handling vs Error Formatting

Bbongjovi4/24/2023
I'm a bit confused from the docs about how I should be handling errors on the server. The Error Handling section refers to handling errors client side right? More specifically, the ORM I'm using suggests delegating error handling to the server implementation. Where the ORM is throwing an instance of its internal NotFoundError class, I need to update the response http code to 404 and also hide the data associated with that error in production. Should I be doing this within the errorFormatter? Thanks! e.g. I could do something like this, but I'm sure there is a better way? Like throwing a new TRPCError somewhere internally between my procedure and the formatter?
import { CustomOrmError, NotFoundError } from 'custom-orm';
import { TRPC_ERROR_CODES_BY_KEY } from '@trpc/server/dist/rpc';

const errorFormatter = ({ shape, error }) => {
if (error.cause instanceof CustomOrmError) {
if (error.cause instanceof NotFoundError) {
return {
message: 'Not found',
code: TRPC_ERROR_CODES_BY_KEY['NOT_FOUND'],
data: {
code: 'NOT_FOUND',
httpStatus: 404,
},
};
}
return {
message: 'Something went wrong...',
code: TRPC_ERROR_CODES_BY_KEY['INTERNAL_SERVER_ERROR'],
data: {
code: 'INTERNAL_SERVER_ERROR',
httpStatus: 500,
},
};
}
return shape;
}
import { CustomOrmError, NotFoundError } from 'custom-orm';
import { TRPC_ERROR_CODES_BY_KEY } from '@trpc/server/dist/rpc';

const errorFormatter = ({ shape, error }) => {
if (error.cause instanceof CustomOrmError) {
if (error.cause instanceof NotFoundError) {
return {
message: 'Not found',
code: TRPC_ERROR_CODES_BY_KEY['NOT_FOUND'],
data: {
code: 'NOT_FOUND',
httpStatus: 404,
},
};
}
return {
message: 'Something went wrong...',
code: TRPC_ERROR_CODES_BY_KEY['INTERNAL_SERVER_ERROR'],
data: {
code: 'INTERNAL_SERVER_ERROR',
httpStatus: 500,
},
};
}
return shape;
}
Nnlucas4/24/2023
Where the ORM is throwing an instance of its internal NotFoundError class, I need to update the response http code to 404 and also hide the data associated with that error in production. Should I be doing this within the errorFormatter?
Yes I think you're doing it all right If you need to handle errors on the server, you can use a middleware to catch the DB's errors and then do whatever you need, then rethrow if you want the error to reach the frontend
Bbongjovi4/25/2023
Great, thanks for clarifying!

Looking for more? Join the community!

Recommended Posts
Next.js body-parsing issueA thead to discuss this issue: https://github.com/trpc/trpc/issues/4243Cannot find module '@trpc/react-query/server' or its corresponding type declarations```ts import { createServerSideHelpers } from '@trpc/react-query/server' ``` This should work, righProcedure with generic input?Is there a way to define a procedure so that it takes input with type parameters, and returns outputDelete item {0: {json:{id: 12324}}}When i try to mutate/delete item with id i am geting this payload `{0: {json:{id: 12324}}}`, withouQuery function depends on a variableIn tRPC v10 accessing a specific path is really easy, but because of that I don't control the query 'useInfiniteQuery' hook disappeared after moving to TurborepoI am using Turborepo with Next.js with the following layout, originally a T3 app - `/apps/app` - `/pconvert the result to date objectsI am not sure if this is even trpcs responsibility but I would like to get my date objects as date oECONNREFUSED with TRPC call on VercelAnyone run into this before? I just deployed my app to Vercel and I run into this error when I triggInvalid ValDoes TRPC string input have a limit? https://prnt.sc/KlXlyoGrzP8P Edit: It was actually from stripis possible to combine next-minimal-starter with react-router-dom ?Hi, I'm trying to combine https://github.com/trpc/trpc/tree/main/examples/next-minimal-starter and rusing same query for entries appHow to use same query for many components? I don't want to request api for many times ;-; I can't pHow can I make a direct fetch on a router endpoint from TRPC in NextJS on client?In the documentation you can use the vanilla TRPC client like this: ``` const bilbo = await clientHow to get unwrapped errors out of proxy clientI'm using sveltekit and in order to redirect from SSR you need to throw an error: https://kit.sveltCan you get the queryClient without using a hook?Can you get the queryClient without using a hook?How can I reset the cursor when using useInfiniteQuery?I have various filters that I can set for the query, but when setting those filters I need to reset How does routing work in tRPC especially when using merged routers?I am having trouble understanding how tRPC lays out routes. Let's say I have the below ```typescrip