api/schema: don't crash on eval error
All checks were successful
checks-impure / test (pull_request) Successful in 1m32s
checks / test (pull_request) Successful in 2m25s

This commit is contained in:
DavHau 2023-11-10 14:27:03 +07:00
parent c194adc181
commit 97ea9312ac
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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