You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/03 16:56:09 UTC

[GitHub] [iceberg] rdblue commented on a diff in pull request #5416: Python: Handle OAuthErrorResponse properly

rdblue commented on code in PR #5416:
URL: https://github.com/apache/iceberg/pull/5416#discussion_r936930139


##########
python/pyiceberg/catalog/rest.py:
##########
@@ -255,39 +265,46 @@ def _split_identifier_for_json(self, identifier: Union[str, Identifier]) -> Dict
         return {"namespace": identifier[:-1], "name": identifier[-1]}
 
     def _handle_non_200_response(self, exc: HTTPError, error_handler: Dict[int, Type[Exception]]):
-        try:
-            response = ErrorResponse(**exc.response.json())
-        except JSONDecodeError:
-            # In the case we don't have a proper response
-            response = ErrorResponse(
-                error=ErrorResponseMessage(
-                    message=f"Could not decode json payload: {exc.response.text}",
-                    type="RESTError",
-                    code=exc.response.status_code,
-                )
-            )
-
+        exception: Type[Exception]
         code = exc.response.status_code
         if code in error_handler:
-            raise error_handler[code](response.error.message) from exc
+            exception = error_handler[code]
         elif code == 400:
-            raise BadRequestError(response.error.message) from exc
+            exception = BadRequestError
         elif code == 401:
-            raise UnauthorizedError(response.error.message) from exc
+            exception = UnauthorizedError
         elif code == 403:
-            raise ForbiddenError(response.error.message) from exc
+            exception = ForbiddenError
         elif code == 422:
-            raise RESTError(response.error.message) from exc
+            exception = RESTError
         elif code == 419:
-            raise AuthorizationExpiredError(response.error.message)
+            exception = AuthorizationExpiredError
         elif code == 501:
-            raise NotImplementedError(response.error.message)
+            exception = NotImplementedError
         elif code == 503:
-            raise ServiceUnavailableError(response.error.message) from exc
+            exception = ServiceUnavailableError
         elif 500 <= code < 600:
-            raise ServerError(response.error.message) from exc
+            exception = ServerError
         else:
-            raise RESTError(response.error.message) from exc
+            exception = RESTError
+
+        try:
+            if exception == OAuthError:
+                # The OAuthErrorResponse has a different format
+                error = OAuthErrorResponse(**exc.response.json())
+                response = str(error.error)
+                if description := error.error_description:
+                    response += f": {description}"
+                if uri := error.error_uri:
+                    response += f" ({uri})"
+            else:
+                error = ErrorResponse(**exc.response.json()).error
+                response = f"{error.type}: {error.message}"
+        except JSONDecodeError:
+            # In the case we don't have a proper response
+            response = f"RESTError: Could not decode json payload: {exc.response.text}"

Review Comment:
   What's the value of `RESTError: ` as a prefix here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org