55
66import asyncio
77from logging import Logger
8- from typing import Any
8+ from typing import Any , Callable
99
1010from pycrdt_websocket .websocket_server import WebsocketServer , YRoom
1111from pycrdt_websocket .ystore import BaseYStore
@@ -16,6 +16,16 @@ class RoomNotFound(LookupError):
1616 pass
1717
1818
19+ def exception_logger (exception : Exception , log : Logger ) -> bool :
20+ """A function that catches any exceptions raised in the websocket
21+ server and logs them.
22+ This protects the websocket server's task group from cancelling
23+ anytime an exception is raised.
24+ """
25+ log .error ("Jupyter Websocket Server: " , exc_info = exception )
26+ return True
27+
28+
1929class JupyterWebsocketServer (WebsocketServer ):
2030 """Ypy websocket server.
2131
@@ -30,9 +40,15 @@ def __init__(
3040 ystore_class : BaseYStore ,
3141 rooms_ready : bool = True ,
3242 auto_clean_rooms : bool = True ,
43+ exception_handler : Callable [[Exception , Logger ], bool ] | None = None ,
3344 log : Logger | None = None ,
3445 ):
35- super ().__init__ (rooms_ready , auto_clean_rooms , log )
46+ super ().__init__ (
47+ rooms_ready = rooms_ready ,
48+ auto_clean_rooms = auto_clean_rooms ,
49+ exception_handler = exception_handler ,
50+ log = log ,
51+ )
3652 self .ystore_class = ystore_class
3753 self .ypatch_nb = 0
3854 self .connected_users : dict [Any , Any ] = {}
0 commit comments