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: HTTPException

HTTP 400 Bad Request exception.

__init__(detail='Bad request')[source]

Args: detail: Error message (default: “Bad request”).

exception genro_asgi.exceptions.HTTPException(status_code, detail='', headers=None)[source]

Bases: Exception

HTTP 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")])
__init__(status_code, detail='', headers=None)[source]

Initialize HTTP exception.

Parameters:
  • status_code (int) – HTTP status code (4xx, 5xx expected)

  • detail (str) – Error detail message (default: “”)

  • headers (dict[str, str] | list[tuple[str, str]] | None) – Response headers as dict or list of tuples (default: None). Dict is converted to list internally to support duplicate names.

exception genro_asgi.exceptions.HTTPForbidden(detail='Forbidden')[source]

Bases: HTTPException

HTTP 403 Forbidden exception.

__init__(detail='Forbidden')[source]

Args: detail: Error message (default: “Forbidden”).

exception genro_asgi.exceptions.HTTPNotFound(detail='Not found')[source]

Bases: HTTPException

HTTP 404 Not Found exception.

__init__(detail='Not found')[source]

Args: detail: Error message (default: “Not found”).

exception genro_asgi.exceptions.HTTPServiceUnavailable(detail='Service unavailable')[source]

Bases: HTTPException

HTTP 503 Service Unavailable exception.

__init__(detail='Service unavailable')[source]

Args: detail: Error message (default: “Service unavailable”).

exception genro_asgi.exceptions.HTTPUnauthorized(detail='Unauthorized')[source]

Bases: HTTPException

HTTP 401 Unauthorized exception.

__init__(detail='Unauthorized')[source]

Args: detail: Error message (default: “Unauthorized”).

exception genro_asgi.exceptions.Redirect(url, status_code=302)[source]

Bases: HTTPException

HTTP redirect exception (302 by default).

url

Target redirect URL (set as Location header).

status_code

HTTP status (302, 301, 307, 308).

__init__(url, status_code=302)[source]

Args: url: Target URL for the Location header. status_code: HTTP redirect status code (default: 302).

exception genro_asgi.exceptions.WebSocketDisconnect(code=1000, reason='')[source]

Bases: Exception

Raised 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}")
__init__(code=1000, reason='')[source]

Initialize disconnect exception.

Parameters:
  • code (int) – WebSocket close code (default: 1000)

  • reason (str) – Close reason (default: “”)

exception genro_asgi.exceptions.WebSocketException(code=1000, reason='')[source]

Bases: Exception

WebSocket 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")
__init__(code=1000, reason='')[source]

Initialize WebSocket exception.

Parameters:
  • code (int) – WebSocket close code (default: 1000)

  • reason (str) – Close reason message (default: “”)