瀏覽代碼

Put failure types at top level

Kirk Trombley 4 年之前
父節點
當前提交
1bd7899c73
共有 1 個文件被更改,包括 38 次插入0 次删除
  1. 38 0
      lib/rollbot/failure.py

+ 38 - 0
lib/rollbot/failure.py

@@ -0,0 +1,38 @@
+from enum import Enum, auto
+
+
+class RollbotFailureException(BaseException):
+    def __init__(self, failure):
+        super().__init__()
+        self.failure = failure
+
+
+class RollbotFailure(Enum):
+    INVALID_COMMAND = auto()
+    MISSING_SUBCOMMAND = auto()
+    INVALID_SUBCOMMAND = auto()
+    INVALID_ARGUMENTS = auto()
+    SERVICE_DOWN = auto()
+    PERMISSIONS = auto()
+    INTERNAL_ERROR = auto()
+
+    def get_debugging(self):
+        debugging = {}
+        reason = getattr(self, "reason", None)
+        if reason is not None:
+            debugging["explain"] = reason
+        exception = getattr(self, "exception", None)
+        if exception is not None:
+            debugging["exception"] = exception
+        return debugging
+
+    def with_reason(self, reason):
+        self.reason = reason
+        return self
+
+    def with_exception(self, exception):
+        self.exception = exception
+        return self
+
+    def raise_exc(self):
+        raise RollbotFailureException(self)