|
@@ -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
|