From 72c31d0c2459324ca63caa939620b83cfed2107b Mon Sep 17 00:00:00 2001 From: Jeremy McCormick Date: Thu, 9 May 2024 14:45:27 -0500 Subject: [PATCH] Improve error handling --- src/jeremymfastapiexample/handlers/external.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/jeremymfastapiexample/handlers/external.py b/src/jeremymfastapiexample/handlers/external.py index 45a44de..caacd5c 100644 --- a/src/jeremymfastapiexample/handlers/external.py +++ b/src/jeremymfastapiexample/handlers/external.py @@ -77,20 +77,15 @@ async def get_schema( logger.info("Request for SDM schema", schema_url=schema_url) try: response = await http_client.get(schema_url) - if response.status_code == 404: - raise HTTPException( - status_code=404, detail=f"URL not found: {schema_url}" - ) - if response.status_code != 200: - raise HTTPException( - status_code=500, - detail="Github returned an error while fetching schema" - f" {name}", - ) + response.raise_for_status() except httpx.RequestError as e: raise HTTPException( status_code=500, - detail=f"Unknown error occurred while fetching schema {name}", + detail=f"Unknown error occurred while fetching {schema_url}", + ) from e + except httpx.HTTPStatusError as e: + raise HTTPException( + status_code=e.response.status_code, detail=f"{e!s}" ) from e data: dict[str, Any] = yaml.safe_load(response.text) return Schema(**data)