Skip to content

OpenAPI Error Handling ​

Before you begin, please review our Error Handling guide. This document shows you how to align your error responses with OpenAPI standards.

Default Error Mappings ​

By default, oRPC maps common error codes to standard HTTP status codes:

Error CodeHTTP Status CodeMessage
BAD_REQUEST400Bad Request
UNAUTHORIZED401Unauthorized
FORBIDDEN403Forbidden
NOT_FOUND404Not Found
METHOD_NOT_SUPPORTED405Method Not Supported
NOT_ACCEPTABLE406Not Acceptable
TIMEOUT408Request Timeout
CONFLICT409Conflict
PRECONDITION_FAILED412Precondition Failed
PAYLOAD_TOO_LARGE413Payload Too Large
UNSUPPORTED_MEDIA_TYPE415Unsupported Media Type
UNPROCESSABLE_CONTENT422Unprocessable Content
TOO_MANY_REQUESTS429Too Many Requests
CLIENT_CLOSED_REQUEST499Client Closed Request
INTERNAL_SERVER_ERROR500Internal Server Error
NOT_IMPLEMENTED501Not Implemented
BAD_GATEWAY502Bad Gateway
SERVICE_UNAVAILABLE503Service Unavailable
GATEWAY_TIMEOUT504Gateway Timeout

Any error not defined above defaults to HTTP status 500 with the error code used as the message.

Customizing Errors ​

You can override the default mappings by specifying a custom status and message when creating an error:

ts
const example = os
  .errors({
    RANDOM_ERROR: {
      status: 503, // <-- override default status
      message: 'Default error message', // <-- override default message
    },
  })
  .handler(() => {
    throw new ORPCError('ANOTHER_RANDOM_ERROR', {
      status: 502, // <-- override default status
      message: 'Custom error message', // <-- override default message
    })
  })

Released under the MIT License.