This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new 46bf3b709e Python: Catch ValidationError on invalid REST response 
(#5897)
46bf3b709e is described below

commit 46bf3b709eefb30c1d08f7d444a90f6429360ee5
Author: Fokko Driesprong <[email protected]>
AuthorDate: Mon Oct 10 11:36:41 2022 +0200

    Python: Catch ValidationError on invalid REST response (#5897)
    
    * Python: Catch ValidationError on invalid REST response
    
    If an implementation of the rest catalog returns something different than
    we expect, we just propagate the ValidationError, which subclasses the
    ValueError. When using the CLI, it will tell you that the URI is not set,
    but actually it is, it is just invalid.
    
    This will return the json to the user, and also tell what's wrong with the
    response.
    
    * Fix the formatting
---
 python/pyiceberg/catalog/rest.py  |  8 +++++++-
 python/tests/catalog/test_rest.py | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/python/pyiceberg/catalog/rest.py b/python/pyiceberg/catalog/rest.py
index 23d0ccf942..09f5793f3c 100644
--- a/python/pyiceberg/catalog/rest.py
+++ b/python/pyiceberg/catalog/rest.py
@@ -26,7 +26,7 @@ from typing import (
 )
 
 import requests
-from pydantic import Field
+from pydantic import Field, ValidationError
 from requests import HTTPError
 
 from pyiceberg import __version__
@@ -300,6 +300,12 @@ class RestCatalog(Catalog):
         except JSONDecodeError:
             # In the case we don't have a proper response
             response = f"RESTError {exc.response.status_code}: Could not 
decode json payload: {exc.response.text}"
+        except ValidationError as e:
+            # In the case we don't have a proper response
+            errs = ", ".join(err["msg"] for err in e.errors())
+            response = (
+                f"RESTError {exc.response.status_code}: Received unexpected 
JSON Payload: {exc.response.text}, errors: {errs}"
+            )
 
         raise exception(response) from exc
 
diff --git a/python/tests/catalog/test_rest.py 
b/python/tests/catalog/test_rest.py
index e97f8ad50f..270bad85f1 100644
--- a/python/tests/catalog/test_rest.py
+++ b/python/tests/catalog/test_rest.py
@@ -28,6 +28,7 @@ from pyiceberg.exceptions import (
     NoSuchNamespaceError,
     NoSuchTableError,
     OAuthError,
+    RESTError,
     TableAlreadyExistsError,
 )
 from pyiceberg.schema import Schema
@@ -113,6 +114,20 @@ def test_token_401(rest_mock: Mocker):
     assert message in str(e.value)
 
 
+def test_token_401_oauth_error(rest_mock: Mocker):
+    """This test returns a OAuth error instead of an OpenAPI error"""
+    message = """RESTError 401: Received unexpected JSON Payload: {"error": 
"invalid_client", "error_description": "Invalid credentials"}, errors: value is 
not a valid dict"""
+    rest_mock.post(
+        f"{TEST_URI}v1/oauth/tokens",
+        json={"error": "invalid_client", "error_description": "Invalid 
credentials"},
+        status_code=401,
+    )
+
+    with pytest.raises(RESTError) as e:
+        RestCatalog("rest", uri=TEST_URI, credential=TEST_CREDENTIALS)
+    assert message in str(e.value)
+
+
 def test_list_tables_200(rest_mock: Mocker):
     namespace = "examples"
     rest_mock.get(

Reply via email to