From 11e1c51866522ddd4538ba5118dcda8c2e80e074 Mon Sep 17 00:00:00 2001 From: NickGoog <66492516+NickGoog@users.noreply.github.com> Date: Mon, 29 Jul 2024 12:41:45 -0400 Subject: [PATCH 1/4] fix: Fail gracefully if could not import rpc_status module --- google/api_core/exceptions.py | 16 ++++++++++++---- tests/unit/test_exceptions.py | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index d4cb9973..927dd5b2 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -28,16 +28,21 @@ from google.rpc import error_details_pb2 + +def _warn_could_not_import_grpcio_status(): + warnings.warn( + "Please install grpcio-status to obtain helpful grpc error messages.", + ImportWarning, + ) + + try: import grpc try: from grpc_status import rpc_status except ImportError: # pragma: NO COVER - warnings.warn( - "Please install grpcio-status to obtain helpful grpc error messages.", - ImportWarning, - ) + _warn_could_not_import_grpcio_status() rpc_status = None except ImportError: # pragma: NO COVER grpc = None @@ -560,6 +565,9 @@ def _is_informative_grpc_error(rpc_exc): def _parse_grpc_error_details(rpc_exc): + if not rpc_status: # pragma: NO COVER + _warn_could_not_import_grpcio_status() + return [], None try: status = rpc_status.from_call(rpc_exc) except NotImplementedError: # workaround diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 07a36817..7c1e12a1 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -392,4 +392,4 @@ def test_error_details_from_grpc_response_unknown_error(): exception.reason is None and exception.domain is None and exception.metadata is None - ) + ) \ No newline at end of file From 4367fe8573fd82b60648fbbea2daf2a5934d124e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 7 Aug 2024 12:31:22 -0400 Subject: [PATCH 2/4] revert --- tests/unit/test_exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 7c1e12a1..07a36817 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -392,4 +392,4 @@ def test_error_details_from_grpc_response_unknown_error(): exception.reason is None and exception.domain is None and exception.metadata is None - ) \ No newline at end of file + ) From 3b4c12bb64eb4b90235562587fdc379062b4aecf Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 7 Aug 2024 16:38:16 +0000 Subject: [PATCH 3/4] add pragma NO COVER for warning to be emitted on a non-standard install --- google/api_core/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index 927dd5b2..ceb85a64 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -33,7 +33,7 @@ def _warn_could_not_import_grpcio_status(): warnings.warn( "Please install grpcio-status to obtain helpful grpc error messages.", ImportWarning, - ) + ) # pragma: NO COVER try: From 5ef36cf03f57b8cab158da841cc090484552fb1e Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Wed, 7 Aug 2024 16:40:10 +0000 Subject: [PATCH 4/4] lint --- google/api_core/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index ceb85a64..74f46ef3 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -33,7 +33,7 @@ def _warn_could_not_import_grpcio_status(): warnings.warn( "Please install grpcio-status to obtain helpful grpc error messages.", ImportWarning, - ) # pragma: NO COVER + ) # pragma: NO COVER try: