Error Handling
If a procedure fails we trigger a function with information about the procedure & ctx.
Example with Next.js
export default trpcNext.createNextApiHandler({
// [...]
onError({ error }) {
console.error('Error:', error);
if (error.code === 'INTERNAL_SERVER_ERROR') {
// send to bug reporting
}
},
});
All properties sent to onError()
{
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 error
export default trpcNext.createNextApiHandler({
// [...]
onError({ error }) {
console.error('Error:', error);
console.log('Original error thrown', error.cause);
},
});
Error helpers
import { 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"
// "PRECONDITION_FAILED"