Skip to content

Customizing Error Response Format

If you need to customize the error response format to match your application's requirements, you can use rootInterceptors in the handler.

WARNING

Avoid combining this with Type‑Safe Error Handling, as the OpenAPI Specification Generator does not yet support custom error response formats.

ts
import { isORPCErrorJson, isORPCErrorStatus } from '@orpc/client'

const handler = new OpenAPIHandler(router, {
  rootInterceptors: [
    async ({ next }) => {
      const result = await next()

      if (
        result.matched
        && isORPCErrorStatus(result.response.status)
        && isORPCErrorJson(result.response.body)
      ) {
        return {
          ...result,
          response: {
            ...result.response,
            body: {
              ...result.response.body,
              message: 'custom error shape',
            },
          },
        }
      }

      return result
    },
  ],
})

Released under the MIT License.