vm-manager: More error handling

This commit is contained in:
Qubasa
2024-01-02 06:23:55 +01:00
parent 1fc524e53e
commit 01977b2e2a
3 changed files with 25 additions and 20 deletions

View File

@@ -10,7 +10,7 @@ from clan_cli.flakes.inspect import FlakeConfig, inspect_flake
from ..clan_uri import ClanURI from ..clan_uri import ClanURI
from ..dirs import user_history_file from ..dirs import user_history_file
from ..locked_open import read_history_file, write_history_file from ..locked_open import read_history_file, write_history_file
from ..errors import ClanError
class EnhancedJSONEncoder(json.JSONEncoder): class EnhancedJSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any: def default(self, o: Any) -> Any:
@@ -56,8 +56,7 @@ def list_history() -> list[HistoryEntry]:
parsed[i] = merge_dicts(p, p["settings"]) parsed[i] = merge_dicts(p, p["settings"])
logs = [HistoryEntry(**p) for p in parsed] logs = [HistoryEntry(**p) for p in parsed]
except (json.JSONDecodeError, TypeError) as ex: except (json.JSONDecodeError, TypeError) as ex:
print("Failed to load history. Invalid JSON.") raise ClanError(f"History file at {user_history_file()} is corrupted") from ex
print(f"{user_history_file()}: {ex}")
return logs return logs

View File

@@ -4,7 +4,9 @@ from pathlib import Path
from typing import Any from typing import Any
import gi import gi
from clan_cli import history from clan_cli.history.list import list_history
from .errors.show_error import show_error_dialog
gi.require_version("GdkPixbuf", "2.0") gi.require_version("GdkPixbuf", "2.0")
@@ -64,24 +66,27 @@ def get_initial_vms(
) -> list[VM]: ) -> list[VM]:
vm_list = [] vm_list = []
# Execute `clan flakes add <path>` to democlan for this to work try:
for entry in history.list.list_history(): # Execute `clan flakes add <path>` to democlan for this to work
icon = assets.loc / "placeholder.jpeg" for entry in list_history():
if entry.flake.icon is not None: icon = assets.loc / "placeholder.jpeg"
icon = entry.flake.icon if entry.flake.icon is not None:
icon = entry.flake.icon
status = False status = False
if entry.flake.flake_url in running_vms: if entry.flake.flake_url in running_vms:
status = True status = True
base = VMBase( base = VMBase(
icon=icon, icon=icon,
name=entry.flake.clan_name, name=entry.flake.clan_name,
url=entry.flake.flake_url, url=entry.flake.flake_url,
status=status, status=status,
_flake_attr=entry.flake.flake_attr, _flake_attr=entry.flake.flake_attr,
) )
vm_list.append(VM(base=base)) vm_list.append(VM(base=base))
except Exception as e:
show_error_dialog(e)
# start/end slices can be used for pagination # start/end slices can be used for pagination
return vm_list[start:end] return vm_list[start:end]

View File

@@ -33,6 +33,7 @@ class OverviewWindow(Gtk.ApplicationWindow):
set_selected=self.set_selected, set_selected=self.set_selected,
selected_vm=None, selected_vm=None,
) )
# Add named stacks # Add named stacks
self.stack.add_titled(clan_list, "list", "List") self.stack.add_titled(clan_list, "list", "List")
self.stack.add_titled( self.stack.add_titled(