Merge pull request #4615 from FinnStutzenstein/silenceWsSendErrosAfterTermination

Silence errors after sending to a closed connection
This commit is contained in:
Emanuel Schütze 2019-04-26 16:10:16 +02:00 committed by GitHub
commit 2a10dd4675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ from typing import Any, Dict, List, Optional
import jsonschema import jsonschema
from channels.generic.websocket import AsyncJsonWebsocketConsumer from channels.generic.websocket import AsyncJsonWebsocketConsumer
from websockets.exceptions import ConnectionClosed
from .autoupdate import AutoupdateFormat from .autoupdate import AutoupdateFormat
from .cache import element_cache from .cache import element_cache
@ -20,16 +21,23 @@ class ProtocollAsyncJsonWebsocketConsumer(AsyncJsonWebsocketConsumer):
content: Any, content: Any,
id: Optional[str] = None, id: Optional[str] = None,
in_response: Optional[str] = None, in_response: Optional[str] = None,
silence_errors: Optional[bool] = True,
) -> None: ) -> None:
""" """
Sends the data with the type. Sends the data with the type.
If silence_errors is True (default), all ConnectionClosed errors
during sending will be ignored.
""" """
out = {"type": type, "content": content} out = {"type": type, "content": content}
if id: if id:
out["id"] = id out["id"] = id
if in_response: if in_response:
out["in_response"] = in_response out["in_response"] = in_response
try:
await super().send_json(out) await super().send_json(out)
except ConnectionClosed as e:
if not silence_errors:
raise e
async def receive_json(self, content: Any) -> None: async def receive_json(self, content: Any) -> None:
""" """