Browse Source

handle all errors without exiting

user 6 months ago
parent
commit
994fac4858
1 changed files with 19 additions and 7 deletions
  1. 19 7
      main.py

+ 19 - 7
main.py

@@ -2,6 +2,7 @@
 from urllib.parse import urlparse
 import os
 import time
+import traceback
 import asyncio
 
 import simplematrixbotlib as botlib
@@ -85,11 +86,22 @@ async def on_message(room, event):
         }
       })
 
+async def main():
+    asr.load_model()
+    while True:
+        try:
+            await bot.run()
+        except (asyncio.exceptions.TimeoutError, aiohttp.ClientError) as e:
+            print(f"Network issue: {e}")
+            traceback.print_exc()
+            print("Network issue, restarting...")
+            await asyncio.sleep(5)
+        except Exception as e:
+            print(f"Unexpected error: {e}")
+            traceback.print_exc()
+            print("Unexpected error, restarting...")
+            await asyncio.sleep(5)
+
 if __name__ == "__main__":
-  asr.load_model()
-  try:
-    bot.run()
-  except asyncio.exceptions.TimeoutError as e:
-    print(e)
-    print("Timeout, restarting...")
-    time.sleep(5)
+    asyncio.run(main())
+