Skip to content

Commit 9b87a0f

Browse files
committed
fix: Fail gracefully if could not import rpc_status module
1 parent 3c5e034 commit 9b87a0f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

google/api_core/exceptions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@
2828

2929
from google.rpc import error_details_pb2
3030

31+
32+
def _warn_could_not_import_grpcio_status():
33+
warnings.warn(
34+
"Please install grpcio-status to obtain helpful grpc error messages.",
35+
ImportWarning,
36+
)
37+
38+
3139
try:
3240
import grpc
3341

3442
try:
3543
from grpc_status import rpc_status
3644
except ImportError: # pragma: NO COVER
37-
warnings.warn(
38-
"Please install grpcio-status to obtain helpful grpc error messages.",
39-
ImportWarning,
40-
)
45+
_warn_could_not_import_grpcio_status()
4146
rpc_status = None
4247
except ImportError: # pragma: NO COVER
4348
grpc = None
@@ -560,6 +565,9 @@ def _is_informative_grpc_error(rpc_exc):
560565

561566

562567
def _parse_grpc_error_details(rpc_exc):
568+
if not rpc_status: # pragma: NO COVER
569+
_warn_could_not_import_grpcio_status()
570+
return [], None
563571
try:
564572
status = rpc_status.from_call(rpc_exc)
565573
except NotImplementedError: # workaround

tests/unit/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,4 @@ def test_error_details_from_grpc_response_unknown_error():
392392
exception.reason is None
393393
and exception.domain is None
394394
and exception.metadata is None
395-
)
395+
)

0 commit comments

Comments
 (0)