home · contact · privacy
Improve exception handling. master
authorChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 16:15:19 +0000 (17:15 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 24 Nov 2024 16:15:19 +0000 (17:15 +0100)
ytplom/misc.py

index 809787a29c0ba3b5a513f7065c4d2db2af65c915..a5fbeb8174389084e4b94353bead4f8eee2a566c 100644 (file)
@@ -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,