Exceptions
plateforme.core.api.exceptions
This module provides utilities for managing exceptions within the Plateforme framework's API using FastAPI and Starlette features.
EXCEPTION_HANDLERS
module-attribute
EXCEPTION_HANDLERS: dict[
type[Exception],
Callable[[Request, Exception], Response],
] = {
DatabaseError: database_exception_handler,
SessionError: session_exception_handler,
}
A dictionary of exception handlers for the Plateforme application.
HTTPException
HTTPException(
status_code: Annotated[
int,
Doc(
"\n HTTP status code to send to the client.\n "
),
],
detail: Annotated[
Any,
Doc(
"\n Any data to be sent to the client in the `detail` key of the JSON\n response.\n "
),
] = None,
headers: Annotated[
Optional[Dict[str, str]],
Doc(
"\n Any headers to send to the client in the response.\n "
),
] = None,
)
Bases: HTTPException
An HTTP exception you can raise in your own code to show errors to the client.
This is for client errors, invalid authentication, invalid data, etc. Not for server errors in your code.
Read more about it in the FastAPI docs for Handling Errors.
Example
from fastapi import FastAPI, HTTPException
app = FastAPI()
items = {"foo": "The Foo Wrestlers"}
@app.get("/items/{item_id}")
async def read_item(item_id: str):
if item_id not in items:
raise HTTPException(status_code=404, detail="Item not found")
return {"item": items[item_id]}
Source code in .venv/lib/python3.12/site-packages/fastapi/exceptions.py
WebSocketException
WebSocketException(
code: Annotated[
int,
Doc(
"\n A closing code from the\n [valid codes defined in the specification](https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1).\n "
),
],
reason: Annotated[
Union[str, None],
Doc(
"\n The reason to close the WebSocket connection.\n\n It is UTF-8-encoded data. The interpretation of the reason is up to the\n application, it is not specified by the WebSocket specification.\n\n It could contain text that could be human-readable or interpretable\n by the client code, etc.\n "
),
] = None,
)
Bases: WebSocketException
A WebSocket exception you can raise in your own code to show errors to the client.
This is for client errors, invalid authentication, invalid data, etc. Not for server errors in your code.
Read more about it in the FastAPI docs for WebSockets.
Example
from typing import Annotated
from fastapi import (
Cookie,
FastAPI,
WebSocket,
WebSocketException,
status,
)
app = FastAPI()
@app.websocket("/items/{item_id}/ws")
async def websocket_endpoint(
*,
websocket: WebSocket,
session: Annotated[str | None, Cookie()] = None,
item_id: str,
):
if session is None:
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Session cookie is: {session}")
await websocket.send_text(f"Message text was: {data}, for item ID: {item_id}")
Source code in .venv/lib/python3.12/site-packages/fastapi/exceptions.py
database_exception_handler
database_exception_handler(
request: Request, exc: Exception
) -> JSONResponse
Handle database exceptions.
Source code in .venv/lib/python3.12/site-packages/plateforme/core/api/exceptions.py
session_exception_handler
session_exception_handler(
request: Request, exc: Exception
) -> JSONResponse
Handle database session exceptions.