From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 24 Nov 2024 16:15:19 +0000 (+0100)
Subject: Improve exception handling.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/tasks?a=commitdiff_plain;h=4afb87e8588f4a2268148ec89c7ab0a643d10a63;p=ytplom

Improve exception handling.
---

diff --git a/ytplom/misc.py b/ytplom/misc.py
index 809787a..a5fbeb8 100644
--- a/ytplom/misc.py
+++ b/ytplom/misc.py
@@ -122,8 +122,12 @@ VIDEO_FLAGS: dict[FlagName, FlagsInt] = {
 }
 
 
-class NotFoundException(BaseException):
-    """Call on DB fetches finding less than expected."""
+class NotFoundException(Exception):
+    """Call on expected data missing, e.g. DB fetches finding nothing."""
+
+
+class CantWriteException(Exception):
+    """Call when there's an obstacle to expected writing of data."""
 
 
 def _ensure_expected_dirs(expected_dirs: list[PathStr]) -> None:
@@ -134,7 +138,7 @@ def _ensure_expected_dirs(expected_dirs: list[PathStr]) -> None:
             makedirs(dir_name)
         elif not isdir(dir_name):
             msg = f'at expected directory path {dir_name} found non-directory'
-            raise Exception(msg)
+            raise CantWriteException(msg)
 
 
 class DatabaseConnection:
@@ -183,7 +187,8 @@ class DbData:
         sql = SqlText(f'SELECT * FROM {cls._table_name} WHERE id = ?')
         row = conn.exec(sql, (id_,)).fetchone()
         if not row:
-            raise NotFoundException
+            msg = f'no entry found for ID "{id_}" in table {cls._table_name}'
+            raise NotFoundException(msg)
         return cls._from_table_row(row)
 
     @classmethod
@@ -310,7 +315,7 @@ class VideoFile(DbData):
         sql = SqlText(f'SELECT * FROM {cls._table_name} WHERE yt_id = ?')
         row = conn.exec(sql, (yt_id,)).fetchone()
         if not row:
-            raise NotFoundException
+            raise NotFoundException(f'no entry for file to Youtube ID {yt_id}')
         return cls._from_table_row(row)
 
     @property
@@ -737,8 +742,8 @@ class TaskHandler(BaseHTTPRequestHandler):
                 self._send_last_playlist_update()
             else:  # e.g. for /
                 self._send_playlist()
-        except NotFoundException:
-            self._send_http(b'not found', code=404)
+        except NotFoundException as e:
+            self._send_http(bytes(str(e), 'utf8'), code=404)
 
     def _send_rendered_template(self,
                                 tmpl_name: PathStr,