get rid of ValueError

This commit is contained in:
Jörg Thalheim
2024-09-03 18:07:36 +02:00
parent 403b9cf2cc
commit f18771364c
11 changed files with 34 additions and 22 deletions

View File

@@ -29,6 +29,7 @@ from pathlib import Path
from typing import Any
from clan_cli.api.modules import Frontmatter, extract_frontmatter, get_roles
from clan_cli.errors import ClanError
# Get environment variables
CLAN_CORE_PATH = Path(os.environ["CLAN_CORE_PATH"])
@@ -153,11 +154,11 @@ options_head = "\n## Module Options\n"
def produce_clan_core_docs() -> None:
if not CLAN_CORE_DOCS:
msg = f"Environment variables are not set correctly: $CLAN_CORE_DOCS={CLAN_CORE_DOCS}"
raise ValueError(msg)
raise ClanError(msg)
if not OUT:
msg = f"Environment variables are not set correctly: $out={OUT}"
raise ValueError(msg)
raise ClanError(msg)
# A mapping of output file to content
core_outputs: dict[str, str] = {}
@@ -228,15 +229,15 @@ clan_modules_descr = """Clan modules are [NixOS modules](https://wiki.nixos.org/
def produce_clan_modules_docs() -> None:
if not CLAN_MODULES:
msg = f"Environment variables are not set correctly: $out={CLAN_MODULES}"
raise ValueError(msg)
raise ClanError(msg)
if not CLAN_CORE_PATH:
msg = f"Environment variables are not set correctly: $CLAN_CORE_PATH={CLAN_CORE_PATH}"
raise ValueError(msg)
raise ClanError(msg)
if not OUT:
msg = f"Environment variables are not set correctly: $out={OUT}"
raise ValueError(msg)
raise ClanError(msg)
with Path(CLAN_MODULES).open() as f:
links: dict[str, str] = json.load(f)

View File

@@ -217,7 +217,7 @@ def main() -> None:
case "network":
if args.network_id is None:
msg = "network_id parameter is required"
raise ValueError(msg)
raise ClanError(msg)
controller = create_network_controller()
identity = controller.identity
network_id = controller.networkid
@@ -227,7 +227,7 @@ def main() -> None:
network_id = args.network_id
case _:
msg = f"unknown mode {args.mode}"
raise ValueError(msg)
raise ClanError(msg)
ip = compute_zerotier_ip(network_id, identity)
args.identity_secret.write_text(identity.private)

View File

@@ -11,6 +11,7 @@ from typing import (
cast,
)
from clan_cli.errors import ClanError
from gi.repository import GLib, GObject
log = logging.getLogger(__name__)
@@ -94,7 +95,7 @@ class GObjApi:
if fn_name in self._obj_registry:
msg = f"Function '{fn_name}' already registered"
raise ValueError(msg)
raise ClanError(msg)
self._obj_registry[fn_name] = obj
def check_signature(self, fn_signatures: dict[str, inspect.Signature]) -> None:
@@ -121,7 +122,7 @@ class GObjApi:
log.error(f"Expected signature: {exp_signature}")
log.error(f"Actual signature: {got_signature}")
msg = f"Overwritten method '{m_name}' has different signature than the implementation"
raise ValueError(msg)
raise ClanError(msg)
def get_obj(self, fn_name: str) -> type[ImplFunc]:
result = self._obj_registry.get(fn_name, None)
@@ -131,7 +132,7 @@ class GObjApi:
plain_fn = self._methods.get(fn_name, None)
if plain_fn is None:
msg = f"Method '{fn_name}' not found in Api"
raise ValueError(msg)
raise ClanError(msg)
class GenericFnRuntime(ImplFunc[..., Any]):
def __init__(self) -> None:

View File

@@ -110,10 +110,10 @@ API.register(open_file)
def register(self, fn: Callable[..., T]) -> Callable[..., T]:
if fn.__name__ in self._registry:
msg = f"Function {fn.__name__} already registered"
raise ValueError(msg)
raise ClanError(msg)
if fn.__name__ in self._orig_signature:
msg = f"Function {fn.__name__} already registered"
raise ValueError(msg)
raise ClanError(msg)
# make copy of original function
self._orig_signature[fn.__name__] = signature(fn)

View File

@@ -176,7 +176,7 @@ def set_service_instance(
if module_name not in service_keys:
msg = f"{module_name} is not a valid Service attribute. Expected one of {', '.join(service_keys)}."
raise ValueError(msg)
raise ClanError(msg)
inventory = load_inventory_json(base_path)
target_type = get_args(get_type_hints(Service)[module_name])[1]

View File

@@ -146,7 +146,7 @@ def run_cmd(programs: list[str], cmd: list[str]) -> list[str]:
for program in programs:
if not Programs.is_allowed(program):
msg = f"Program not allowed: {program}"
raise ValueError(msg)
raise ClanError(msg)
if os.environ.get("IN_NIX_SANDBOX"):
return cmd
missing_packages = [

View File

@@ -2,6 +2,10 @@ import subprocess
import tempfile
class Error(Exception):
pass
def is_valid_age_key(secret_key: str) -> bool:
# Run the age-keygen command with the -y flag to check the key format
result = subprocess.run(
@@ -11,7 +15,7 @@ def is_valid_age_key(secret_key: str) -> bool:
if result.returncode == 0:
return True
msg = f"Invalid age key: {secret_key}"
raise ValueError(msg)
raise Error(msg)
def is_valid_ssh_key(secret_key: str, ssh_pub: str) -> bool:
@@ -27,7 +31,7 @@ def is_valid_ssh_key(secret_key: str, ssh_pub: str) -> bool:
if result.returncode == 0:
if result.stdout != ssh_pub:
msg = f"Expected '{ssh_pub}' got '{result.stdout}' for ssh key: {secret_key}"
raise ValueError(msg)
raise Error(msg)
return True
msg = f"Invalid ssh key: {secret_key}"
raise ValueError(msg)
raise Error(msg)

View File

@@ -5,6 +5,7 @@ from typing import Any, Generic, TypeVar
import gi
gi.require_version("Gio", "2.0")
from clan_cli.errors import ClanError
from gi.repository import Gio, GObject
log = logging.getLogger(__name__)
@@ -77,7 +78,7 @@ class GKVStore(GObject.GObject, Gio.ListModel, Generic[K, V]):
key = self.key_gen(item)
if key in self._items:
msg = "Key already exists in the dictionary"
raise ValueError(msg)
raise ClanError(msg)
if position < 0 or position > len(self._items):
msg = "Index out of range"
raise IndexError(msg)

View File

@@ -6,6 +6,7 @@ from typing import Any, TypeVar
import gi
from clan_cli.clan_uri import ClanURI
from clan_cli.errors import ClanError
from clan_vm_manager.components.gkvstore import GKVStore
from clan_vm_manager.components.interfaces import ClanConfig
@@ -259,7 +260,7 @@ class ClanList(Gtk.Box):
vm = ClanStore.use().set_logging_vm(target)
if vm is None:
msg = f"VM {target} not found"
raise ValueError(msg)
raise ClanError(msg)
views = ViewStack.use().view
# Reset the logs view
@@ -267,7 +268,7 @@ class ClanList(Gtk.Box):
if logs is None:
msg = "Logs view not found"
raise ValueError(msg)
raise ClanError(msg)
name = vm.machine.name if vm.machine else "Unknown"

View File

@@ -0,0 +1,2 @@
class Error(Exception):
pass

View File

@@ -1,5 +1,7 @@
from urllib.parse import urlparse
from moonlight_sunshine_accept.errors import Error
def parse_moonlight_uri(uri: str) -> tuple[str, int | None]:
print(uri)
@@ -11,10 +13,10 @@ def parse_moonlight_uri(uri: str) -> tuple[str, int | None]:
parsed = urlparse(uri)
if parsed.scheme != "moonlight":
msg = f"Invalid moonlight URI: {uri}"
raise ValueError(msg)
raise Error(msg)
hostname = parsed.hostname
if hostname is None:
msg = f"Invalid moonlight URI: {uri}"
raise ValueError
raise Error(msg)
port = parsed.port
return (hostname, port)