Browse Source

implemented a proper error handler

digital 7 years ago
parent
commit
5a2cc53c71
1 changed files with 13 additions and 10 deletions
  1. 13 10
      network/__init__.py

+ 13 - 10
network/__init__.py

@@ -38,7 +38,19 @@ lch = logging.getLogger(__name__+".chandler")
 lschat = logging.getLogger(__name__+".server.chat")
 lcchat = logging.getLogger(__name__+".client.chat")
 
-_tasks = []
+def error_handler(
+        func,args=(),kwargs={},logger=None,log_text="error",async_=False):
+    if not logger:
+        raise TypeError(
+            "No logger specified but this error handler needs one!")
+    try:
+        if async_
+            curio.run(func(*args,**kwargs))
+        else:
+            func(*args,**kwargs)
+    except Exception as exc:
+        logger.debug("error occured during "+log_text,exc_info=exc)
+
 
 class ConnHandlerBase(object):
     """
@@ -290,15 +302,6 @@ class Server(object):
         it unregisters itself from atexit, so it doesn't get executed twice
         when it was manually called before to program exits.
         """
-        async def error_handler(
-            func,*args,log_text="error",async_=False,**kwargs):
-            try:
-                if async_:
-                    await func(*args,**kwargs)
-                else:
-                    func(*args,**kwargs)
-            except Exception as exc:
-                lserver.debug("error occured during "+log_text,exc_info=exc)
         atexit.unregister(self.shutdown)
         lserver.info("shutting down server")
         await error_handler(self.handle_tasks.cancel_remaining,