Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/286.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise a proper branch not found error when requesting a node or schema for a branch that doesn't exist.
34 changes: 22 additions & 12 deletions infrahub_sdk/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing_extensions import TypeAlias

from ..exceptions import (
BranchNotFoundError,
InvalidResponseError,
JsonDecodeError,
SchemaNotFoundError,
Expand Down Expand Up @@ -167,6 +168,25 @@

raise ValueError("schema must be a protocol or a string")

@staticmethod
def _parse_schema_response(response: httpx.Response, branch: str) -> MutableMapping[str, Any]:
if response.status_code == 400:
raise BranchNotFoundError(
identifier=branch, message=f"The requested branch was not found on the server [{branch}]"
)
response.raise_for_status()

try:
data: MutableMapping[str, Any] = response.json()
except json.decoder.JSONDecodeError as exc:
raise JsonDecodeError(

Check warning on line 182 in infrahub_sdk/schema/__init__.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema/__init__.py#L181-L182

Added lines #L181 - L182 were not covered by tests
message=f"Invalid Schema response received from the server at {response.url}: {response.text} [{response.status_code}] ",
content=response.text,
url=str(response.url),
) from exc

return data


class InfrahubSchema(InfrahubSchemaBase):
def __init__(self, client: InfrahubClient):
Expand Down Expand Up @@ -420,16 +440,8 @@
url = f"{self.client.address}/api/schema?{query_params}"

response = await self.client._get(url=url, timeout=timeout)
response.raise_for_status()

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
data = self._parse_schema_response(response=response, branch=branch)

nodes: MutableMapping[str, MainSchemaTypesAPI] = {}
for node_schema in data.get("nodes", []):
Expand Down Expand Up @@ -648,9 +660,7 @@
query_params = urlencode(url_parts)
url = f"{self.client.address}/api/schema?{query_params}"
response = self.client._get(url=url, timeout=timeout)
response.raise_for_status()

data: MutableMapping[str, Any] = response.json()
data = self._parse_schema_response(response=response, branch=branch)

nodes: MutableMapping[str, MainSchemaTypesAPI] = {}
for node_schema in data.get("nodes", []):
Expand Down
18 changes: 15 additions & 3 deletions tests/integration/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# import pytest
import pytest

from infrahub_sdk import InfrahubClient
from infrahub_sdk.exceptions import BranchNotFoundError
from infrahub_sdk.testing.docker import TestInfrahubDockerClient


# from infrahub.core.schema import core_models
# from infrahub.server import app
#
# from infrahub_sdk import Config, InfrahubClient
# from infrahub_sdk.schema import NodeSchemaAPI
#
# from .conftest import InfrahubTestClient
#
#
#
#
class TestInfrahubSchema(TestInfrahubDockerClient):
async def test_query_schema_for_branch_not_found(self, client: InfrahubClient):
with pytest.raises(BranchNotFoundError) as exc:
await client.all(kind="BuiltinTag", branch="I-do-not-exist")

assert str(exc.value) == "The requested branch was not found on the server [I-do-not-exist]"


# class TestInfrahubSchema:
# @pytest.fixture(scope="class")
# async def client(self):
Expand Down