Skip to main content

Exceptions

The exceptions module provides typed HTTP errors. Raise them from handlers and OxAPY converts them into JSON error responses.

Hierarchy

Exception
├── ClientError # base for all 4xx errors
│ ├── BadRequestError → 400
│ ├── UnauthorizedError → 401
│ ├── ForbiddenError → 403
│ ├── NotFoundError → 404
│ └── ConflictError → 409
└── InternalError → 500

Usage

from oxapy import Router, get, exceptions


@get("/users/{user_id}")
def get_user(request, user_id: int):
user = find_user(user_id)
if user is None:
raise exceptions.NotFoundError("User not found")
return user

Response: 404 with body:

{"detail": "User not found"}

Status mapping

ExceptionStatus
UnauthorizedError401
ForbiddenError403
NotFoundError404
ConflictError409
Other ClientError subclasses400
Anything else (including InternalError)500