File tree 2 files changed +35
-4
lines changed
2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change 28
28
29
29
from google .rpc import error_details_pb2
30
30
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
+
31
39
try :
32
40
import grpc
33
41
34
42
try :
35
43
from grpc_status import rpc_status
36
44
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 ()
41
46
rpc_status = None
42
47
except ImportError : # pragma: NO COVER
43
48
grpc = None
@@ -560,6 +565,11 @@ def _is_informative_grpc_error(rpc_exc):
560
565
561
566
562
567
def _parse_grpc_error_details (rpc_exc ):
568
+ # pragma: NO COVER
569
+ if not rpc_status :
570
+ _warn_could_not_import_grpcio_status ()
571
+ return [], None
572
+ # pragma: NO COVER
563
573
try :
564
574
status = rpc_status .from_call (rpc_exc )
565
575
except NotImplementedError : # workaround
Original file line number Diff line number Diff line change @@ -393,3 +393,24 @@ def test_error_details_from_grpc_response_unknown_error():
393
393
and exception .domain is None
394
394
and exception .metadata is None
395
395
)
396
+
397
+
398
+ @pytest .mark .skipif (grpc is None , reason = "gRPC not importable" )
399
+ def test_from_grpc_error_warns_if_not_grpc ():
400
+ try :
401
+ from grpc_status import rpc_status
402
+ del rpc_status # Unused.
403
+ # Warning won't trigger.
404
+ return
405
+ except ImportError : # pragma: NO COVER
406
+ # Proceed to rest of test.
407
+ pass
408
+
409
+ error = mock .create_autospec (grpc .Call , instance = True )
410
+
411
+ with mock .patch ("warnings.warn" , autospec = True ) as mock_warn :
412
+ exceptions .from_grpc_error (error )
413
+ mock_warn .assert_called_with (
414
+ "Please install grpcio-status to obtain helpful grpc error messages." ,
415
+ ImportWarning ,
416
+ )
You can’t perform that action at this time.
0 commit comments