ORPCError
ORPCError is the core error class. Throw it from any handler or middleware:
SCREAMING_SNAKE_CASE. The second argument is an options object:
| Option | Type | Description |
|---|---|---|
status | number | HTTP status code (must be 4xx or 5xx) |
message | string | Human-readable error message |
data | any | Extra structured data attached to the error |
cause | unknown | The underlying cause (not sent to the client) |
Standard error codes
oRPC pre-defines common error codes that map to standard HTTP status codes:| Code | Status |
|---|---|
BAD_REQUEST | 400 |
UNAUTHORIZED | 401 |
FORBIDDEN | 403 |
NOT_FOUND | 404 |
METHOD_NOT_SUPPORTED | 405 |
TIMEOUT | 408 |
CONFLICT | 409 |
PRECONDITION_FAILED | 412 |
PAYLOAD_TOO_LARGE | 413 |
UNSUPPORTED_MEDIA_TYPE | 415 |
UNPROCESSABLE_CONTENT | 422 |
TOO_MANY_REQUESTS | 429 |
CLIENT_CLOSED_REQUEST | 499 |
INTERNAL_SERVER_ERROR | 500 |
NOT_IMPLEMENTED | 501 |
BAD_GATEWAY | 502 |
SERVICE_UNAVAILABLE | 503 |
GATEWAY_TIMEOUT | 504 |
Typed errors with .errors()
Declare the errors a procedure can throw using .errors(). This provides typed error constructors inside the handler and propagates type information to the client and OpenAPI spec.
errors object in the handler is a typed map of constructors. Each constructor accepts an optional options object without the status field (since that is set in the error map).
Error maps on builders
You can define shared errors on a builder so they are available to all derived procedures:isDefinedError()
On the client side, use isDefinedError() to narrow an unknown error to a typed ORPCError that was declared in the error map:
