Fix broad exception handling and future.result() call

- Call future.result() instead of bare property access in test (#596)
- Catch (ValueError, TypeError) instead of broad Exception in
  response model serialization (#597)
- Catch WebSocketDisconnect instead of broad Exception in
  websocket chat example (#598)

Closes #596, closes #597, closes #598

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 15:25:12 -04:00
parent 29d0621d98
commit 6f9c87d71c
3 changed files with 8 additions and 3 deletions
+3 -1
View File
@@ -1,5 +1,7 @@
# WebSocket chat room example.
# https://responder.kennethreitz.org/tutorial-websockets.html
from starlette.websockets import WebSocketDisconnect
import responder
api = responder.API()
@@ -47,7 +49,7 @@ async def chat(ws):
message = await ws.receive_text()
for client in connected:
await client.send_text(message)
except Exception: # noqa: S110
except WebSocketDisconnect:
pass
finally:
connected.discard(ws)
+1 -1
View File
@@ -192,7 +192,7 @@ class Route(BaseRoute):
try:
validated = resp_model(**response.media)
response.media = validated.model_dump()
except Exception: # noqa: S110
except (ValueError, TypeError):
pass # Don't break the response if serialization fails
# Run after-request hooks
+4 -1
View File
@@ -78,7 +78,10 @@ def test_background_task_exception(capsys):
raise ValueError("task failed")
future = failing_task()
future.result # wait for completion # noqa: B018
try:
future.result() # wait for completion
except ValueError:
pass
time.sleep(0.2) # let the done callback fire
captured = capsys.readouterr()