From 42dd7b0125d677eebd39ef2ea0d184c796d143d1 Mon Sep 17 00:00:00 2001 From: Seunghyeon Yang <61305117+1yangsh@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:30:50 +0900 Subject: [PATCH] REST: Raise NoSuchNamespaceError for 404 on create_view and register The REST spec defines a 404 from createView, registerTable and registerView as "the namespace specified does not exist", and create_table already maps it to NoSuchNamespaceError. These three methods had no 404 mapping, so a missing namespace surfaced as a generic RESTError. --- pyiceberg/catalog/rest/__init__.py | 7 +-- tests/catalog/test_rest.py | 82 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 2ad3684c9a..d7ce852b62 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -1368,7 +1368,7 @@ def create_view( try: response.raise_for_status() except HTTPError as exc: - _handle_non_200_response(exc, {409: ViewAlreadyExistsError}) + _handle_non_200_response(exc, {404: NoSuchNamespaceError, 409: ViewAlreadyExistsError}) view_response = ViewResponse.model_validate_json(response.text) return self._response_to_view(self.identifier_to_tuple(identifier), view_response) @@ -1388,6 +1388,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str, o Raises: TableAlreadyExistsError: If the table already exists + NoSuchNamespaceError: If the namespace does not exist """ self._check_endpoint(Capability.V1_REGISTER_TABLE) namespace_and_table = self._split_identifier_for_path(identifier) @@ -1405,7 +1406,7 @@ def register_table(self, identifier: str | Identifier, metadata_location: str, o try: response.raise_for_status() except HTTPError as exc: - _handle_non_200_response(exc, {409: TableAlreadyExistsError}) + _handle_non_200_response(exc, {404: NoSuchNamespaceError, 409: TableAlreadyExistsError}) table_response = TableResponse.model_validate_json(response.text) return self._response_to_table(self.identifier_to_tuple(identifier), table_response) @@ -1893,7 +1894,7 @@ def register_view(self, identifier: str | Identifier, metadata_location: str) -> try: response.raise_for_status() except HTTPError as exc: - _handle_non_200_response(exc, {409: ViewAlreadyExistsError}) + _handle_non_200_response(exc, {404: NoSuchNamespaceError, 409: ViewAlreadyExistsError}) view_response = ViewResponse.model_validate_json(response.text) return self._response_to_view(self.identifier_to_tuple(identifier), view_response) diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py index 3d2b64da92..6e021e10eb 100644 --- a/tests/catalog/test_rest.py +++ b/tests/catalog/test_rest.py @@ -1853,6 +1853,28 @@ def test_create_table_409(rest_mock: Mocker, table_schema_simple: Schema) -> Non assert "Table already exists" in str(e.value) +def test_register_table_404(rest_mock: Mocker) -> None: + rest_mock.post( + f"{TEST_URI}v1/namespaces/fokko/register", + json={ + "error": { + "message": "Namespace does not exist: fokko in warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e", + "type": "NoSuchNamespaceException", + "code": 404, + } + }, + status_code=404, + request_headers=TEST_HEADERS, + ) + + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + with pytest.raises(NoSuchNamespaceError) as e: + catalog.register_table( + identifier=("fokko", "registered_table"), metadata_location="s3://warehouse/database/table/metadata.json" + ) + assert "Namespace does not exist" in str(e.value) + + def test_create_view_200(rest_mock: Mocker, table_schema_simple: Schema, example_view_metadata_rest_json: dict[str, Any]) -> None: rest_mock.post( f"{TEST_URI}v1/namespaces/fokko/views", @@ -1942,6 +1964,66 @@ def test_create_view_409( assert "View already exists" in str(e.value) +def test_register_view_404(rest_mock: Mocker) -> None: + rest_mock.head( + f"{TEST_URI}v1/namespaces/fokko/tables/registered_view", + status_code=404, + request_headers=TEST_HEADERS, + ) + rest_mock.post( + f"{TEST_URI}v1/namespaces/fokko/register-view", + json={ + "error": { + "message": "Namespace does not exist: fokko in warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e", + "type": "NoSuchNamespaceException", + "code": 404, + } + }, + status_code=404, + request_headers=TEST_HEADERS, + ) + + catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN) + with pytest.raises(NoSuchNamespaceError) as e: + catalog.register_view( + identifier=("fokko", "registered_view"), metadata_location="s3://warehouse/database/view/metadata.json" + ) + assert "Namespace does not exist" in str(e.value) + + +def test_create_view_404( + rest_mock: Mocker, + table_schema_simple: Schema, +) -> None: + rest_mock.post( + f"{TEST_URI}v1/namespaces/fokko/views", + json={ + "error": { + "message": "Namespace does not exist: fokko in warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e", + "type": "NoSuchNamespaceException", + "code": 404, + } + }, + status_code=404, + request_headers=TEST_HEADERS, + ) + + with pytest.raises(NoSuchNamespaceError) as e: + RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).create_view( + identifier=("fokko", "fokko2"), + schema=table_schema_simple, + view_version=ViewVersion( + schema_id=1, + summary={"engine-name": "spark", "engineVersion": "3.3"}, + representations=[], + default_namespace=[], + ), + location=None, + properties={"owner": "fokko"}, + ) + assert "Namespace does not exist" in str(e.value) + + def test_load_view_200(rest_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None: rest_mock.get( f"{TEST_URI}v1/namespaces/fokko/views/view",