Error Handling

Whenever an error occurs in a procedure, tRPC responds to the client with an object that includes an “error” property. This property contains all the information that you need to handle the error in the client.

Here’s an example error response caused by a bad request input:

  1. {
  2. "id": null,
  3. "error": {
  4. "message": "\"password\" must be at least 4 characters",
  5. "code": -32600,
  6. "data": {
  7. "code": "BAD_REQUEST",
  8. "httpStatus": 400,
  9. "stack": "...",
  10. "path": "user.changepassword"
  11. }
  12. }
  13. }

Error codes

tRPC defines a list of error codes that each represent a different type of error and response with a different HTTP code.

CodeDescriptionHTTP code
BAD_REQUESTThe server cannot or will not process the request due to something that is perceived to be a client error.400
UNAUTHORIZEDThe client request has not been completed because it lacks valid authentication credentials for the requested resource.401
FORBIDDENThe server was unauthorized to access a required data source, such as a REST API.403
NOT_FOUNDThe server cannot find the requested resource.404
TIMEOUTThe server would like to shut down this unused connection.408
CONFLICTThe server request resource conflict with the current state of the target resource.409
PRECONDITION_FAILEDAccess to the target resource has been denied.412
PAYLOAD_TOO_LARGERequest entity is larger than limits defined by server.413
METHOD_NOT_SUPPORTEDThe server knows the request method, but the target resource doesn’t support this method.405
CLIENT_CLOSED_REQUESTAccess to the resource has been denied.499
INTERNAL_SERVER_ERRORAn unspecified error occurred.500

Throwing errors

tRPC provides an error subclass, TRPCError, which you can use to represent an error that occurred inside a procedure.

For example, throwing this error:

server.ts

  1. import * as trpc from '@trpc/server';
  2. const appRouter = trpc.router().query('hello', {
  3. resolve: () => {
  4. throw new trpc.TRPCError({
  5. code: 'INTERNAL_SERVER_ERROR',
  6. message: 'An unexpected error occurred, please try again later.',
  7. // optional: pass the oroginal error to retain stack trace
  8. cause: theError,
  9. });
  10. },
  11. });
  12. // [...]

Results to the following response:

  1. {
  2. "id": null,
  3. "error": {
  4. "message": "An unexpected error occurred, please try again later.",
  5. "code": -32603,
  6. "data": {
  7. "code": "INTERNAL_SERVER_ERROR",
  8. "httpStatus": 500,
  9. "stack": "...",
  10. "path": "hello"
  11. }
  12. }
  13. }

Handling errors

All errors that occur in a procedure go through the onError method before being sent to the client. Here you can handle or change errors.

pages/api/trpc/[trpc].ts

  1. export default trpcNext.createNextApiHandler({
  2. // ...
  3. onError({ error, type, path, input, ctx, req }) {
  4. console.error('Error:', error);
  5. if (error.code === 'INTERNAL_SERVER_ERROR') {
  6. // send to bug reporting
  7. }
  8. },
  9. });

The onError parameter is an object that contains all information about the error and the context it occures in:

  1. {
  2. error: TRPCError; // the original error
  3. type: 'query' | 'mutation' | 'subscription' | 'unknown';
  4. path: string | undefined; // path of the procedure that was triggered
  5. input: unknown;
  6. ctx: Context | undefined;
  7. req: BaseRequest; // request object
  8. }