From 97ea9312ac9daa662b4b32bbe2a43d546e0782cc Mon Sep 17 00:00:00 2001 From: DavHau Date: Fri, 10 Nov 2023 14:27:03 +0700 Subject: [PATCH] api/schema: don't crash on eval error --- pkgs/clan-cli/clan_cli/config/schema.py | 3 ++- pkgs/clan-cli/tests/test_machines_api.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_cli/config/schema.py b/pkgs/clan-cli/clan_cli/config/schema.py index 723401d3a..c3a5712b0 100644 --- a/pkgs/clan-cli/clan_cli/config/schema.py +++ b/pkgs/clan-cli/clan_cli/config/schema.py @@ -10,6 +10,7 @@ from clan_cli.dirs import ( nixpkgs_source, specific_flake_dir, ) +from clan_cli.errors import ClanError from clan_cli.nix import nix_eval from ..types import FlakeName @@ -75,5 +76,5 @@ def machine_schema( ) if proc.returncode != 0: print(proc.stderr, file=sys.stderr) - raise Exception(f"Failed to read schema:\n{proc.stderr}") + raise ClanError(f"Failed to read schema:\n{proc.stderr}") return json.loads(proc.stdout) diff --git a/pkgs/clan-cli/tests/test_machines_api.py b/pkgs/clan-cli/tests/test_machines_api.py index 84a9eeb7e..8b0cbddc9 100644 --- a/pkgs/clan-cli/tests/test_machines_api.py +++ b/pkgs/clan-cli/tests/test_machines_api.py @@ -22,6 +22,21 @@ def test_machines(api: TestClient, test_flake: FlakeForTest) -> None: assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]} +@pytest.mark.with_core +def test_schema_errors(api: TestClient, test_flake_with_core: FlakeForTest) -> None: + # make sure that eval errors do not raise an internal server error + response = api.put( + f"/api/{test_flake_with_core.name}/schema", + json={"imports": ["some-inavlid-import"]}, + ) + assert response.status_code == 422 + # expect error to contain "error: string 'some-inavlid-import' doesn't represent an absolute path" + assert ( + "error: string 'some-inavlid-import' doesn't represent an absolute path" + in response.json()["detail"][0]["msg"] + ) + + @pytest.mark.with_core def test_configure_machine(api: TestClient, test_flake_with_core: FlakeForTest) -> None: # ensure error 404 if machine does not exist when accessing the config