Best way to handle WebSocket errors in Django Channels without closing the connection? #2191
-
|
Hey everyone 👋 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey! 👋 async def receive_json(self, content): |
Beta Was this translation helpful? Give feedback.
Hey! 👋
You’re actually on the right track.
For runtime errors (inside receive_json), catching exceptions and sending a structured "error" message back to the client is totally fine — that’s how most production apps handle it.
async def receive_json(self, content):
try:
# your logic here
await self.send_json({"type": "success", "data": "ok"})
except Exception as e:
await self.send_json({"type": "error", "message": str(e)})