Error Handling
If a procedure fails we trigger a function with information about the procedure & ctx.
#
Example with Next.jsexport default trpcNext.createNextApiHandler({ // [...] onError({ error }) { console.error('Error:', error); if (error.code === 'INTERNAL_SERVER_ERROR') { // send to bug reporting } },});
onError()
#
All properties sent to { error: TRPCError; type: 'query' | 'mutation' | 'subscription' | 'unknown'; path: string | undefined; // path of the procedure that was triggered input: unknown; ctx: Context | undefined; req: BaseRequest; // request object}
#
Accessing original errorexport default trpcNext.createNextApiHandler({ // [...] onError({ error }) { console.error('Error:', error); console.log('Original error thrown', error.cause); },});
#
Error helpersimport { TRPCError } from '@trpc/server';
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Optional Message', // optional: pass your thrown error to TRPCError to retain stack trace cause: myError,});
// Some available codes:// // "FORBIDDEN"// "BAD_REQUEST"// "INTERNAL_SERVER_ERROR"// "NOT_FOUND"// "TIMEOUT"