T
tRPC

Use onError to change an application error into a TRPCError?

Use onError to change an application error into a TRPCError?

JJavascriptMick6/2/2023
I want to use the onError handler to change any instance of a custom application error into a TRPCError (I want the HTTP Status code to be a 401 rather than a 500). I tried this but it doesn't work...
onError({error}) {
if (error instanceof AccountLimitError){
error = new TRPCError({ code: 'UNAUTHORIZED', message:error.message })
}
}
onError({error}) {
if (error instanceof AccountLimitError){
error = new TRPCError({ code: 'UNAUTHORIZED', message:error.message })
}
}
nor does this
onError({error}) {
if (error instanceof AccountLimitError){
const trpcError = new TRPCError({ code: 'UNAUTHORIZED', message:error.message })
error = Object.assign(error, trpcError)
}
}
onError({error}) {
if (error instanceof AccountLimitError){
const trpcError = new TRPCError({ code: 'UNAUTHORIZED', message:error.message })
error = Object.assign(error, trpcError)
}
}
I can set individual properties on the error object like error.message but thought there might be an easier way. Has anybody got a nifty hack for this?
Nnlucas6/2/2023
Thrown errors automatically get wrapped with a TRPCError and sent to the frontend Like Alex said, an error formatter is probably what you want if you need to manipulate them
JJavascriptMick6/3/2023
ah nice, thanks guys, yep this works.....
const t = initTRPC.context<Context>().create({
errorFormatter: (opts)=> {
const { shape, error } = opts;
if (!(error.cause instanceof AccountLimitError)) {
return shape;
}
return {
...shape,
data: {
...shape.data,
httpStatus: 401,
code: 'UNAUTHORIZED'
},
};
}
})
const t = initTRPC.context<Context>().create({
errorFormatter: (opts)=> {
const { shape, error } = opts;
if (!(error.cause instanceof AccountLimitError)) {
return shape;
}
return {
...shape,
data: {
...shape.data,
httpStatus: 401,
code: 'UNAUTHORIZED'
},
};
}
})
See example in my boilerplate repo here https://github.com/JavascriptMick/nuxt3-boilerplate/blob/master/server/trpc/trpc.ts

Looking for more? Join the community!

T
tRPC

Use onError to change an application error into a TRPCError?

Join Server
Recommended Posts
Increasing Body 1mb limitHey, Im trying to build an application that allows sending of base64 encoded files to my next.js serNitro and tRPC in vercel-edgeHey there! I hope this is the right place to ask for help. I am trying to deploy an application witHow to retrieve and receive Bigint data to/from TRPC procedureNode: `v16.15.1` I'm trying to return an object which contains an `amount` property from one of my React Router Loaders + tRPC?In Vite project: would it make sense to use React Router loaders with tRPC? I like more the concept TypeError: Cannot read properties of null (reading 'useContext') when using useMutation with TRPC inI'm encountering an error in my Next.js application when trying to use the useMutation hook with TRPInitial websockets getToken() returns null: next-auth + websockets :)Hello! So the way I am trying to authenticate websockets is like this: ``` import { getToken } froCan I get the original type name, instead of the shape, with query?I'm not sure how to explain it, so, here's a screen shot:TRPCClientError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON ErrorHello folks, I'm getting an issue with my integration, I'm a newbie in tRPC and I can't work well mytrpc corsmy sveltekit app is running on https://example.com with tRPC and it's making requests to http://127.T3 app tRPC external callsCurrently i'm just making Post request and formatting my payload to ```{ "0": { "json":Next-auth session not being fetched in tRPC contextNext-auth session not being fetched in tRPC contextuseMutation not handeling arguments correctly.Hello, I have a mutation setup on the server with the input being an object containing the fields emUpdate Clerk Organization from TRPC Router (T3 Stack)Hello, if anyone got an idea on how to handle this I would highly appreciate it. I have some organiHow to not send request in specific condition in useQuery()```js const { data: artists, isLoading } = api.findArtist.useQuery({ name: search, }); ``` TRPCClientError: fetch failedI get the below error when createTRPCProxyClient runs on BE of a Next.JS app hosted on Vercel: ```TinferRouterOutputs not inferringCould anyone point me in the right direction to why my types aren't being inferred here? (Using T3 b