소스 검색

Fixing a bug in the session command and improving general error handling

Kirk Trombley 6 년 전
부모
커밋
492e9a2411
3개의 변경된 파일15개의 추가작업 그리고 7개의 파일을 삭제
  1. 8 3
      src/app.py
  2. 3 0
      src/command_system.py
  3. 4 4
      src/plugins/session.py

+ 8 - 3
src/app.py

@@ -6,7 +6,7 @@ from threading import Thread
 from flask import Flask, request, render_template
 
 from config import BOTS_LOOKUP
-from command_system import RollbotMessage
+from command_system import RollbotMessage, RollbotResponse, RollbotFailure
 from rollbot import Rollbot
 from util import post_message
 
@@ -70,14 +70,18 @@ def execute():
         return "", 204
 
     if msg.group_id not in BOTS_LOOKUP:
-        app.logger.warn(f"Received message from unknown group ID {msg.group_id}")
+        app.logger.warning(f"Received message from unknown group ID {msg.group_id}")
         return "Invalid group ID", 403
 
 
     def run_command_and_respond():
         app.logger.info(f"Entering command thread for {msg.message_id}")
         t = time.time()
-        response = rollbot.run_command(msg)
+        try:
+            response = rollbot.run_command(msg)
+        except Exception as e:
+            app.logger.error(f"Exception during command execution {e}, for message {msg.message_id}")
+            response = RollbotResponse(msg, failure=RollbotFailure.INTERNAL_ERROR)
 
         if not response.respond:
             app.logger.info(f"Skipping response to message {msg.message_id}")
@@ -93,6 +97,7 @@ def execute():
                 post_message(response.img, bot_id=bot_id)
         else:
             post_message(response.failure_msg, bot_id=bot_id)
+            app.logger.warning(f"Failed command response: {response}")
 
         t = time.time() - t
         app.logger.info(f"Exiting command thread for {msg.message_id} after {t:.3f}s")

+ 3 - 0
src/command_system.py

@@ -70,6 +70,7 @@ class RollbotFailure(Enum):
     INVALID_ARGUMENTS = auto()
     SERVICE_DOWN = auto()
     PERMISSIONS = auto()
+    INTERNAL_ERROR = auto()
 
 
 _RESPONSE_TEMPLATE = """Response{
@@ -109,6 +110,8 @@ class RollbotResponse:
             self.failure_msg = "Sorry - %s relies on a service I couldn't reach!" % self.msg.command
         elif self.failure == RollbotFailure.PERMISSIONS:
             self.failure_msg = "Sorry - you don't have permission to use that command or sub-command in this chat!"
+        elif self.failure == RollbotFailure.INTERNAL_ERROR:
+            self.failure_msg = "Sorry - I encountered an unrecoverable error, please review internal logs."
 
         if self.debugging is not None and "explain" in self.debugging:
             self.failure_msg += " " + self.debugging["explain"]

+ 4 - 4
src/plugins/session.py

@@ -64,15 +64,15 @@ def parse_datetime(arg_text):
             int(hour), int(minute)
         )
 
-    time, _ = pop_arg(arg_text)
+    time, arg_text = pop_arg(arg_text)
     time_parts = time.split(":")
     hour = time_parts[0]
     if len(time_parts) > 1:
         minute = time_parts[1]
 
-    try:
-        ampm = next(args)
-    except StopIteration:
+    ampm, arg_text = pop_arg(arg_text)
+
+    if ampm is None:
         return datetime.datetime(
             int(year), int(month), int(day),
             int(hour), int(minute)