diff --git a/changelog/+jsondecode.changed.md b/changelog/+jsondecode.changed.md new file mode 100644 index 00000000..030c274b --- /dev/null +++ b/changelog/+jsondecode.changed.md @@ -0,0 +1 @@ +Improve error message when a schema received from the server is not JSON valid. The new exception will be of type `infrahub_sdk.exceptions.JsonDecodeError` instead of `json.decoder.JSONDecodeError` \ No newline at end of file diff --git a/infrahub_sdk/schema/__init__.py b/infrahub_sdk/schema/__init__.py index 080d7237..3a8773bb 100644 --- a/infrahub_sdk/schema/__init__.py +++ b/infrahub_sdk/schema/__init__.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import json from collections.abc import MutableMapping from enum import Enum from time import sleep @@ -13,6 +14,7 @@ from ..exceptions import ( InvalidResponseError, + JsonDecodeError, SchemaNotFoundError, ValidationError, ) @@ -420,7 +422,14 @@ async def _fetch( response = await self.client._get(url=url, timeout=timeout) response.raise_for_status() - data: MutableMapping[str, Any] = response.json() + try: + data: MutableMapping[str, Any] = response.json() + except json.decoder.JSONDecodeError as exc: + raise JsonDecodeError( + message=f"Invalid Schema response received from the server at {response.url}: {response.text} [{response.status_code}] ", + content=response.text, + url=response.url, + ) from exc nodes: MutableMapping[str, MainSchemaTypesAPI] = {} for node_schema in data.get("nodes", []):