add error handling to endpoint
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
@@ -15,9 +16,21 @@ router = APIRouter()
|
|||||||
async def inspect_flake_attrs(url: str) -> FlakeAttrResponse:
|
async def inspect_flake_attrs(url: str) -> FlakeAttrResponse:
|
||||||
cmd = nix_flake_show(url)
|
cmd = nix_flake_show(url)
|
||||||
stdout = await run_cmd(cmd)
|
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())
|
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)
|
return FlakeAttrResponse(flake_attrs=flake_attrs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user