Browse Source

Handle missing links

Getty Ritter 4 years ago
parent
commit
97afe0415e
2 changed files with 15 additions and 1 deletions
  1. 11 0
      lc/error.py
  2. 4 1
      lc/model.py

+ 11 - 0
lc/error.py

@@ -47,6 +47,17 @@ class NoSuchUser(LCException):
         return 404
 
 
+@dataclass
+class NoSuchLink(LCException):
+    link_id: int
+
+    def __str__(self):
+        return f"No link {self.link_id} exists."
+
+    def http_code(self) -> int:
+        return 404
+
+
 @dataclass
 class BadPassword(LCException):
     name: str

+ 4 - 1
lc/model.py

@@ -85,7 +85,10 @@ class User(Model):
         return link_views, pagination
 
     def get_link(self, link_id: int) -> "Link":
-        return Link.get((Link.user == self) & (Link.id == link_id))
+        try:
+            return Link.get((Link.user == self) & (Link.id == link_id))
+        except Link.DoesNotExist:
+            raise e.NoSuchLink(link_id)
 
     def get_tag(self, tag_name: str) -> "Tag":
         return Tag.get((Tag.user == self) & (Tag.name == tag_name))