Skip to content

Commit

Permalink
fix: Fail gracefully if could not import rpc_status module
Browse files Browse the repository at this point in the history
  • Loading branch information
NickGoog committed Aug 7, 2024
1 parent 3c5e034 commit 11e1c51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions google/api_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)

0 comments on commit 11e1c51

Please sign in to comment.