Exceptions
HTTP and WebSocket exception classes.
HTTP and WebSocket exception classes.
- Classes:
HTTPException — HTTP error response (status_code, detail, headers) HTTPForbidden, HTTPNotFound, HTTPUnauthorized — common HTTP errors WebSocketException — close WebSocket with error code WebSocketDisconnect — signal: client disconnected (not an error) Redirect — HTTP redirect (301/302/307/308)
- exception genro_asgi.exceptions.HTTPBadRequest(detail='Bad request')[source]
Bases:
HTTPExceptionHTTP 400 Bad Request exception.
- exception genro_asgi.exceptions.HTTPException(status_code, detail='', headers=None)[source]
Bases:
ExceptionHTTP exception with status code and detail.
Raise this in handlers to return an HTTP error response. The framework will catch this and convert it to an appropriate HTTP response with the given status code, detail, and headers.
- status_code
HTTP status code (expected 4xx or 5xx, not validated)
- detail
Error detail message
- headers
Response headers as list of tuples (supports duplicate names)
Example
>>> raise HTTPException(404, detail="User not found") >>> raise HTTPException(401, headers={"WWW-Authenticate": "Bearer"}) >>> raise HTTPException(400, headers=[("Set-Cookie", "a=1"), ("Set-Cookie", "b=2")])
- exception genro_asgi.exceptions.HTTPForbidden(detail='Forbidden')[source]
Bases:
HTTPExceptionHTTP 403 Forbidden exception.
- exception genro_asgi.exceptions.HTTPNotFound(detail='Not found')[source]
Bases:
HTTPExceptionHTTP 404 Not Found exception.
Bases:
HTTPExceptionHTTP 503 Service Unavailable exception.
Args: detail: Error message (default: “Service unavailable”).
- exception genro_asgi.exceptions.HTTPUnauthorized(detail='Unauthorized')[source]
Bases:
HTTPExceptionHTTP 401 Unauthorized exception.
- exception genro_asgi.exceptions.Redirect(url, status_code=302)[source]
Bases:
HTTPExceptionHTTP redirect exception (302 by default).
- url
Target redirect URL (set as Location header).
- status_code
HTTP status (302, 301, 307, 308).
- exception genro_asgi.exceptions.WebSocketDisconnect(code=1000, reason='')[source]
Bases:
ExceptionRaised when a WebSocket is disconnected by the client.
This is not an error, just a signal that the connection was closed. The framework raises this when a receive operation fails because the client has disconnected.
- code
WebSocket close code from client
- reason
Close reason from client (if any)
Example
>>> try: ... data = await websocket.receive_text() ... except WebSocketDisconnect as e: ... print(f"Client disconnected: {e.code}")
- exception genro_asgi.exceptions.WebSocketException(code=1000, reason='')[source]
Bases:
ExceptionWebSocket exception with close code and reason.
Raise this to close a WebSocket connection with an error code. The framework will catch this and send a close frame with the given code and reason.
- code
WebSocket close code (1000-4999, not validated)
- reason
Close reason message
Example
>>> raise WebSocketException(code=4000, reason="Invalid message")