Skip to content

Commit c84017d

Browse files
fix: raise precise exception in rest_catalog list_namespaces (#1977)
<!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> <!-- Closes #${GITHUB_ISSUE_ID} --> # Rationale for this change `list_namespaces` should raise a `NoSuchNamespaceException` instead of a generic `RestException` in case of a 404 response. https://github.com/apache/iceberg/blob/main/open-api/rest-catalog-open-api.yaml#L279-L287 Also this is in line with the Java implementation of [NamespaceErrorHandler](https://github.com/apache/iceberg/blob/9fb80b7167e58a2daef80f4a1a09f223b870c030/core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java#L182) used by `[listNamespaces()](https://github.com/apache/iceberg/blob/9fb80b7167e58a2daef80f4a1a09f223b870c030/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java#L584)` # Are these changes tested? I added a unit test for it # Are there any user-facing changes? <!-- In the case of user-facing changes, please add the changelog label. -->
1 parent 23143e8 commit c84017d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pyiceberg/catalog/rest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ def list_namespaces(self, namespace: Union[str, Identifier] = ()) -> List[Identi
816816
try:
817817
response.raise_for_status()
818818
except HTTPError as exc:
819-
self._handle_non_200_response(exc, {})
819+
self._handle_non_200_response(exc, {404: NoSuchNamespaceError})
820820

821821
return ListNamespaceResponse.model_validate_json(response.text).namespaces
822822

tests/catalog/test_rest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,24 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
555555
]
556556

557557

558+
def test_list_namespace_with_parent_404(rest_mock: Mocker) -> None:
559+
rest_mock.get(
560+
f"{TEST_URI}v1/namespaces?parent=some_namespace",
561+
json={
562+
"error": {
563+
"message": "Namespace provided in the `parent` query parameter is not found",
564+
"type": "NoSuchNamespaceException",
565+
"code": 404,
566+
}
567+
},
568+
status_code=404,
569+
request_headers=TEST_HEADERS,
570+
)
571+
572+
with pytest.raises(NoSuchNamespaceError):
573+
RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).list_namespaces(("some_namespace",))
574+
575+
558576
@pytest.mark.filterwarnings(
559577
"ignore:Deprecated in 0.8.0, will be removed in 1.0.0. Iceberg REST client is missing the OAuth2 server URI:DeprecationWarning"
560578
)

0 commit comments

Comments
 (0)