add error handling to endpoint
Some checks failed
checks-impure / test (pull_request) Successful in 12s
checks / test (pull_request) Failing after 1m22s

This commit is contained in:
Johannes Kirschbauer 2023-10-03 11:12:06 +02:00
parent a461024f82
commit 3c11eece4d
Signed by: hsjobeki
GPG Key ID: F62ED8B8BF204685

View File

@ -1,4 +1,5 @@
import json
from json.decoder import JSONDecodeError
from pathlib import Path
from fastapi import APIRouter, HTTPException
@ -15,9 +16,21 @@ router = APIRouter()
async def inspect_flake_attrs(url: str) -> FlakeAttrResponse:
cmd = nix_flake_show(url)
stdout = await run_cmd(cmd)
data = json.loads(stdout)
nixos_configs = data["nixosConfigurations"]
data: dict[str,dict] = {}
try:
data = json.loads(stdout)
except JSONDecodeError:
raise HTTPException(status_code=422, detail=f"Could not load flake.")
nixos_configs = data.get("nixosConfigurations",{})
flake_attrs = list(nixos_configs.keys())
if not flake_attrs:
raise HTTPException(status_code=422, detail="No entry or no attribute: nixosConfigurations")
return FlakeAttrResponse(flake_attrs=flake_attrs)