Skip to content

Commit

Permalink
Add reason to WebSocketException
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed May 10, 2022
1 parent 48469a8 commit ade417f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions starlette/exceptions.py
Expand Up @@ -28,12 +28,13 @@ def __repr__(self) -> str:


class WebSocketException(Exception):
def __init__(self, code: int) -> None:
def __init__(self, code: int, reason: typing.Optional[str] = None) -> None:
self.code = code
self.reason = reason or ""

def __repr__(self) -> str:
class_name = self.__class__.__name__
return f"{class_name}(code={self.code!r})"
return f"{class_name}(code={self.code!r}, reason={self.reason!r})"


class ExceptionMiddleware:
Expand Down Expand Up @@ -133,4 +134,4 @@ def http_exception(self, request: Request, exc: HTTPException) -> Response:
async def websocket_exception(
self, websocket: WebSocket, exc: WebSocketException
) -> None:
await websocket.close(code=exc.code)
await websocket.close(code=exc.code, reason=exc.reason)

0 comments on commit ade417f

Please sign in to comment.