Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Re-export Starlette's WebSocketException and add it to docs #5629

Merged
merged 2 commits into from Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/en/docs/advanced/websockets.md
Expand Up @@ -112,17 +112,15 @@ In WebSocket endpoints you can import from `fastapi` and use:

They work the same way as for other FastAPI endpoints/*path operations*:

```Python hl_lines="58-65 68-83"
```Python hl_lines="66-77 76-91"
{!../../../docs_src/websockets/tutorial002.py!}
```

!!! info
In a WebSocket it doesn't really make sense to raise an `HTTPException`. So it's better to close the WebSocket connection directly.
As this is a WebSocket it doesn't really make sense to raise an `HTTPException`, instead we raise a `WebSocketException`.

You can use a closing code from the <a href="https://tools.ietf.org/html/rfc6455#section-7.4.1" class="external-link" target="_blank">valid codes defined in the specification</a>.

In the future, there will be a `WebSocketException` that you will be able to `raise` from anywhere, and add exception handlers for it. It depends on the <a href="https://github.com/encode/starlette/pull/527" class="external-link" target="_blank">PR #527</a> in Starlette.

### Try the WebSockets with dependencies

If your file is named `main.py`, run your application with:
Expand Down
12 changes: 10 additions & 2 deletions docs_src/websockets/tutorial002.py
@@ -1,6 +1,14 @@
from typing import Union

from fastapi import Cookie, Depends, FastAPI, Query, WebSocket, status
from fastapi import (
Cookie,
Depends,
FastAPI,
Query,
WebSocket,
WebSocketException,
status,
)
from fastapi.responses import HTMLResponse

app = FastAPI()
Expand Down Expand Up @@ -61,7 +69,7 @@ async def get_cookie_or_token(
token: Union[str, None] = Query(default=None),
):
if session is None and token is None:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
raise WebSocketException(code=status.WS_1008_POLICY_VIOLATION)
return session or token


Expand Down
1 change: 1 addition & 0 deletions fastapi/__init__.py
Expand Up @@ -8,6 +8,7 @@
from .background import BackgroundTasks as BackgroundTasks
from .datastructures import UploadFile as UploadFile
from .exceptions import HTTPException as HTTPException
from .exceptions import WebSocketException as WebSocketException
from .param_functions import Body as Body
from .param_functions import Cookie as Cookie
from .param_functions import Depends as Depends
Expand Down
1 change: 1 addition & 0 deletions fastapi/exceptions.py
Expand Up @@ -3,6 +3,7 @@
from pydantic import BaseModel, ValidationError, create_model
from pydantic.error_wrappers import ErrorList
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.exceptions import WebSocketException as WebSocketException # noqa: F401


class HTTPException(StarletteHTTPException):
Expand Down