Exceptions API Reference
LightAPI v2 defines two framework-level exceptions. All other error responses use standard Starlette/HTTP status codes.
ConfigurationError
Raised at startup when a RestEndpoint or LightApi configuration is invalid.
Common causes:
- A field annotation uses a type not in the type map and
exclude=Trueis not set. Meta.serializerhas bothfieldsandread/writeset (mutually exclusive).Meta.paginationuses an invalidstylevalue orpage_size < 1.Meta.cachehasttl < 1.- An async engine is used without the
lightapi[async]extras installed. - A YAML
database.urlreferences an unset environment variable. - The
lightapi.yamldocument fails Pydantic v2 schema validation (invalid field type, missingroute, unknown class name, etc.).
from lightapi import RestEndpoint, ConfigurationError
try:
class BadEndpoint(RestEndpoint):
data: list # list is not in the type map
except ConfigurationError as e:
print(e)
# RestEndpoint 'BadEndpoint': annotation 'list' on field 'data' is not in the type map.
SerializationError
Raised when a database row cannot be converted to a serialisable dict. This typically happens when a column value has an unexpected type.
HTTP-level errors
For HTTP errors returned to clients, LightAPI uses standard Starlette responses:
| Situation | Status |
|---|---|
| Resource not found | 404 Not Found |
| Version conflict (optimistic locking) | 409 Conflict |
| Validation failure | 422 Unprocessable Entity |
| Unauthenticated | 401 Unauthorized |
| Insufficient permission | 403 Forbidden |
| Method not allowed | 405 Method Not Allowed |
| Server error | 500 Internal Server Error |
To return custom error responses from method overrides, use starlette.responses.JSONResponse: