Skip to content

Commit 66c41d2

Browse files
committed
Raise a proper branch not found exception for branches that doesn't
exist Fixes #286
1 parent 0510d52 commit 66c41d2

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

changelog/286.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise a proper branch not found error when requesting a node or schema for a branch that doesn't exist.

infrahub_sdk/schema/__init__.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing_extensions import TypeAlias
1414

1515
from ..exceptions import (
16+
BranchNotFoundError,
1617
InvalidResponseError,
1718
JsonDecodeError,
1819
SchemaNotFoundError,
@@ -167,6 +168,25 @@ def _get_schema_name(schema: type[SchemaType | SchemaTypeSync] | str) -> str:
167168

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

171+
@staticmethod
172+
def _parse_schema_response(response: httpx.Response, branch: str) -> MutableMapping[str, Any]:
173+
if response.status_code == 400:
174+
raise BranchNotFoundError(
175+
identifier=branch, message=f"The requested branch was not found on the server [{branch}]"
176+
)
177+
response.raise_for_status()
178+
179+
try:
180+
data: MutableMapping[str, Any] = response.json()
181+
except json.decoder.JSONDecodeError as exc:
182+
raise JsonDecodeError(
183+
message=f"Invalid Schema response received from the server at {response.url}: {response.text} [{response.status_code}] ",
184+
content=response.text,
185+
url=str(response.url),
186+
) from exc
187+
188+
return data
189+
170190

171191
class InfrahubSchema(InfrahubSchemaBase):
172192
def __init__(self, client: InfrahubClient):
@@ -420,16 +440,8 @@ async def _fetch(
420440
url = f"{self.client.address}/api/schema?{query_params}"
421441

422442
response = await self.client._get(url=url, timeout=timeout)
423-
response.raise_for_status()
424443

425-
try:
426-
data: MutableMapping[str, Any] = response.json()
427-
except json.decoder.JSONDecodeError as exc:
428-
raise JsonDecodeError(
429-
message=f"Invalid Schema response received from the server at {response.url}: {response.text} [{response.status_code}] ",
430-
content=response.text,
431-
url=response.url,
432-
) from exc
444+
data = self._parse_schema_response(response=response, branch=branch)
433445

434446
nodes: MutableMapping[str, MainSchemaTypesAPI] = {}
435447
for node_schema in data.get("nodes", []):
@@ -648,9 +660,7 @@ def _fetch(self, branch: str, namespaces: list[str] | None = None, timeout: int
648660
query_params = urlencode(url_parts)
649661
url = f"{self.client.address}/api/schema?{query_params}"
650662
response = self.client._get(url=url, timeout=timeout)
651-
response.raise_for_status()
652-
653-
data: MutableMapping[str, Any] = response.json()
663+
data = self._parse_schema_response(response=response, branch=branch)
654664

655665
nodes: MutableMapping[str, MainSchemaTypesAPI] = {}
656666
for node_schema in data.get("nodes", []):

tests/integration/test_schema.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
# import pytest
1+
import pytest
2+
3+
from infrahub_sdk import InfrahubClient
4+
from infrahub_sdk.exceptions import BranchNotFoundError
5+
from infrahub_sdk.testing.docker import TestInfrahubDockerClient
6+
7+
28
# from infrahub.core.schema import core_models
39
# from infrahub.server import app
410
#
5-
# from infrahub_sdk import Config, InfrahubClient
611
# from infrahub_sdk.schema import NodeSchemaAPI
712
#
813
# from .conftest import InfrahubTestClient
914
#
1015
#
1116
#
12-
#
17+
class TestInfrahubSchema(TestInfrahubDockerClient):
18+
async def test_query_schema_for_branch_not_found(self, client: InfrahubClient):
19+
with pytest.raises(BranchNotFoundError) as exc:
20+
await client.all(kind="BuiltinTag", branch="I-do-not-exist")
21+
22+
assert str(exc.value) == "The requested branch was not found on the server [I-do-not-exist]"
23+
24+
1325
# class TestInfrahubSchema:
1426
# @pytest.fixture(scope="class")
1527
# async def client(self):

0 commit comments

Comments
 (0)