浏览代码

Skeleton of with_failure_handling

Kirk Trombley 4 年之前
父节点
当前提交
9469ec3cb1
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      lib/rollbot/command/failure.py

+ 15 - 0
lib/rollbot/command/failure.py

@@ -1,5 +1,8 @@
+from functools import wraps
 from enum import Enum, auto
 
+from ..types import Message, Context, Response, CommandType
+
 
 class RollbotFailureException(BaseException):
     def __init__(self, failure):
@@ -36,3 +39,15 @@ class RollbotFailure(Enum):
 
     def raise_exc(self):
         raise RollbotFailureException(self)
+
+
+def with_failure_handling(fn: CommandType) -> CommandType:
+    @wraps(fn)
+    async def wrapped(message: Message, context: Context):
+        try:
+            await fn()
+        except RollbotFailureException as exc:
+            # TODO handle errors more specifically
+            await context.respond(Response.from_message(message, str(exc.failure)))
+
+    return wrapped