Ver código fonte

Error handling in post_groupme_message

Kirk Trombley 5 anos atrás
pai
commit
50eb7f699e
1 arquivos alterados com 14 adições e 9 exclusões
  1. 14 9
      src/groupme_bot.py

+ 14 - 9
src/groupme_bot.py

@@ -4,6 +4,7 @@ from threading import Thread
 
 from flask import Flask, request
 import requests
+import requests.exceptions
 
 from rollbot import Rollbot, RollbotMessage, RollbotPlugin
 
@@ -34,15 +35,19 @@ dictConfig({
 
 def post_groupme_message(msg, group_id):
     bot_id = BOTS_LOOKUP[group_id]
-    requests.post(
-        "https://api.groupme.com/v3/bots/post",
-        json={
-            "bot_id": bot_id,
-            "text": msg
-        },
-        timeout=10
-    )
-
+    try:
+        requests.post(
+            "https://api.groupme.com/v3/bots/post",
+            json={
+                "bot_id": bot_id,
+                "text": msg
+            },
+            timeout=10
+        )
+    except requests.exceptions.Timeout as t:
+        app.logger.error(f"GroupMe timed out sending {msg} as {bot_id}", exc_info=t)
+    except requests.exceptions.HTTPError as h:
+        app.log_exception(h)
 
 app = Flask(__name__)
 app.config["PROPAGATE_EXCEPTIONS"] = True