Exceptions – HTTPException
und WebSocketException
¶
Dies sind die Exceptions, die Sie auslösen können, um dem Client Fehler zu berichten.
Wenn Sie eine Exception auslösen, wird, wie es bei normalem Python der Fall wäre, der Rest der Ausführung abgebrochen. Auf diese Weise können Sie diese Exceptions von überall im Code werfen, um einen Request abzubrechen und den Fehler dem Client anzuzeigen.
Sie können Folgendes verwenden:
HTTPException
WebSocketException
Diese Exceptions können direkt von fastapi
importiert werden:
from fastapi import HTTPException, WebSocketException
fastapi.HTTPException
¶
HTTPException(status_code, detail=None, headers=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]}
PARAMETER | DESCRIPTION |
---|---|
status_code |
HTTP status code to send to the client.
TYPE:
|
detail |
Any data to be sent to the client in the
TYPE:
|
headers |
Any headers to send to the client in the response.
TYPE:
|
Source code in fastapi/exceptions.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
fastapi.WebSocketException
¶
WebSocketException(code, reason=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}")
PARAMETER | DESCRIPTION |
---|---|
code |
A closing code from the valid codes defined in the specification.
TYPE:
|
reason |
The reason to close the WebSocket connection. It is UTF-8-encoded data. The interpretation of the reason is up to the application, it is not specified by the WebSocket specification. It could contain text that could be human-readable or interpretable by the client code, etc.
TYPE:
|
Source code in fastapi/exceptions.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|