From d04af8d6dcb7abc9fdca3677c0ffea418be71fb1 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Sun, 8 Mar 2026 13:41:35 -0600 Subject: [PATCH] handle errors better --- app.py | 25 +++++++++++++++++++------ database.py | 18 ++++++++++++------ session-mover.py | 0 3 files changed, 31 insertions(+), 12 deletions(-) mode change 100644 => 100755 session-mover.py diff --git a/app.py b/app.py index 54677cf..df681ed 100644 --- a/app.py +++ b/app.py @@ -282,6 +282,12 @@ class SessionMoverApp(App): color: $text; } + #current-selection.error { + background: $error; + color: $text; + text-style: bold; + } + #selection-count { height: auto; padding: 1; @@ -385,10 +391,13 @@ class SessionMoverApp(App): self.db.connect() self.db.load_reference_data() except FileNotFoundError as e: - self.notify(str(e), severity="error", timeout=10) - self.set_timer(1, self.exit) + widget = self.query_one("#current-selection", Static) + widget.add_class("error") + widget.update(f"Error: {e}\n\nPress q to quit.") + self._db_error = True return + self._db_error = False self._project_ids: List[str] = [] self.refresh_project_list() @@ -398,15 +407,14 @@ class SessionMoverApp(App): self.query_one("#archived-toggle", Button).can_focus = False # Setup sessions table (but don't load data yet) - table = self.query_one("#sessions-table") + table = self.query_one("#sessions-table", DataTable) table.zebra_stripes = True table.cursor_type = "row" table.show_cursor = False table.add_columns(" ", "ID", "Title", "Project", "Workspace", "Msgs", "Created") - + # Don't load sessions until a project is selected - status = self.query_one("#current-selection") - status.update("Select a project from the left") + self.query_one("#current-selection", Static).update("Select a project from the left") def on_input_changed(self, event: Input.Changed) -> None: if event.input.id == "filter-input": @@ -655,6 +663,11 @@ class SessionMoverApp(App): self.refresh_sessions() self.notify("Data refreshed", severity="information") + async def action_quit(self) -> None: + """Exit the app, with a non-zero return code if there was a DB error.""" + return_code = 1 if getattr(self, "_db_error", False) else 0 + self.exit(return_code) + def on_unmount(self) -> None: """Cleanup on exit.""" self.db.close() diff --git a/database.py b/database.py index 0a39fed..3682099 100644 --- a/database.py +++ b/database.py @@ -74,7 +74,8 @@ class Database: def load_reference_data(self): """Load projects and workspaces into memory for fast lookups.""" - assert self.conn is not None + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") cursor = self.conn.cursor() # Load projects @@ -125,7 +126,8 @@ class Database: Returns: List of Session objects with computed counts """ - assert self.conn is not None, "Database not connected" + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") cursor = self.conn.cursor() query = """ @@ -205,7 +207,8 @@ class Database: def get_session_counts_by_project(self) -> dict: """Return a dict of project_id -> active session count.""" - assert self.conn is not None + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") cursor = self.conn.cursor() cursor.execute( "SELECT project_id, COUNT(*) FROM session WHERE time_archived IS NULL GROUP BY project_id" @@ -228,7 +231,8 @@ class Database: Returns: (success, list of SQL statements executed) """ - assert self.conn is not None, "Database not connected" + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") cursor = self.conn.cursor() # Verify target project exists @@ -265,7 +269,8 @@ class Database: Returns: (success, list of SQL statements, list of new session IDs) """ - assert self.conn is not None, "Database not connected" + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") import uuid cursor = self.conn.cursor() @@ -355,7 +360,8 @@ class Database: Returns: (success, error_message) """ - assert self.conn is not None, "Database not connected" + if self.conn is None: + raise RuntimeError("Database is not connected. Call connect() first.") cursor = self.conn.cursor() try: diff --git a/session-mover.py b/session-mover.py old mode 100644 new mode 100755