فهرست منبع

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