Litestar Exceptions
Use this skill for domain exception hierarchies, handler registration, and HTTP error response shape.
Code Style Rules
- Centralize domain-to-HTTP translation in exception handlers.
- Keep route handlers free of repetitive try/except blocks.
- Use domain exception classes when services need stable error contracts.
- Keep validation errors aligned with DTO and OpenAPI behavior.
- Use
ProblemDetailsPluginexplicitly when the API contract requires RFC- Native
HTTPExceptionresponses are Litestar's JSON error envelope.
- Native
Quick Reference
- Exception patterns: exceptions.md
- Pair with litestar-auth-guards for permission failures.
- Pair with litestar-data-services for not-found and conflict behavior.
Workflow
- Define a small domain exception hierarchy.
- Register handlers at app config.
- Raise domain exceptions from services or Litestar exceptions from framework boundaries.
- Test response status and payload shape.
Guardrails
- Do not catch exceptions in every handler.
- Do not leak database exception messages to API clients.
- Do not return inconsistent error payloads from neighboring routes.
- Do not replace Litestar validation behavior without a clear API reason.
- Do not describe native
HTTPExceptionresponses as Problem Details unlessProblemDetailsPluginis configured for them.
Validation Checkpoint
- Exceptions have stable status mapping.
- App-level handlers are registered.
- Services do not return sentinel error values.
- Tests cover representative failure responses.
Example
class ApplicationError(HTTPException):
status_code = 500
class ConflictError(ApplicationError):
status_code = 409
References Index
- exceptions.md
Official References
- https://docs.litestar.dev/ - Litestar documentation
- https://docs.litestar.dev/latest/reference/ - Litestar API reference
- https://github.com/litestar-org/litestar/tree/v2.24.0 - Audited Litestar 2.24.0 source