APIException class for all expected errors. Raise it from any layer — the error handler registered in app.py catches it and returns a formatted JSON response.
APIException
Raising errors
ImportAPIException in your service and raise it for expected error conditions:
How it reaches the client
app.py registers APIException with Flask’s error handler:
APIException raised anywhere in the request lifecycle (route, service, repository) is automatically caught and returned as:
Passing extra data
Usepayload to include additional fields in the error response:
Common status codes
| Code | Use case |
|---|---|
400 | Bad request — missing or invalid input |
401 | Unauthorized — missing or invalid token |
403 | Forbidden — valid token but insufficient role |
404 | Not found — resource does not exist |
409 | Conflict — e.g. duplicate email |
422 | Unprocessable — validation failed |
500 | Internal server error (default) |
Handling database errors
Wrap repository calls in the service when you need to translate database exceptions:Unhandled exceptions
Any exception that is not anAPIException will propagate as a 500 Internal Server Error. Use myLogger.exception(e) to log the full traceback before re-raising or wrapping: