Browse Source

Refactor exception family for better utility elsewhere

Getty Ritter 4 years ago
parent
commit
dc4f7d917d
1 changed files with 18 additions and 1 deletions
  1. 18 1
      lc/error.py

+ 18 - 1
lc/error.py

@@ -1,9 +1,26 @@
 from dataclasses import dataclass
 
+class LCException(Exception):
+    def to_json(self) -> dict:
+        return {"error": str(self)}
 
 @dataclass
-class UserExists(Exception):
+class UserExists(LCException):
     name: str
 
     def __str__(self):
         return f"A user named {self.name} already exists."
+
+@dataclass
+class NoSuchUser(LCException):
+    name: str
+
+    def __str__(self):
+        return f"No user named {self.name} exists."
+
+@dataclass
+class BadPassword(LCException):
+    name: str
+
+    def __str__(self):
+        return f"Wrong password for user {self.name}."