@@ -2788,6 +2788,7 @@ async def run(self) -> T:
2788
2788
except PyMongoError as exc :
2789
2789
always_retryable = False
2790
2790
overloaded = False
2791
+ exc_to_check = exc
2791
2792
# Execute specialized catch on read
2792
2793
if self ._is_read :
2793
2794
if isinstance (exc , (ConnectionFailure , OperationFailure )):
@@ -2811,16 +2812,13 @@ async def run(self) -> T:
2811
2812
2812
2813
# Specialized catch on write operation
2813
2814
if not self ._is_read :
2814
- retryable_write_label = False
2815
- if isinstance (exc , ClientBulkWriteException ) and exc .error :
2816
- if isinstance (exc .error , PyMongoError ):
2817
- retryable_write_label = exc .error .has_error_label ("RetryableWriteError" )
2818
- always_retryable = exc .error .has_error_label ("Retryable" )
2819
- overloaded = exc .error .has_error_label ("SystemOverloaded" )
2820
- else :
2821
- retryable_write_label = exc .has_error_label ("RetryableWriteError" )
2822
- always_retryable = exc .has_error_label ("Retryable" )
2823
- overloaded = exc .has_error_label ("SystemOverloaded" )
2815
+ if isinstance (exc , ClientBulkWriteException ) and isinstance (
2816
+ exc .error , PyMongoError
2817
+ ):
2818
+ exc_to_check = exc .error
2819
+ retryable_write_label = exc_to_check .has_error_label ("RetryableWriteError" )
2820
+ always_retryable = exc_to_check .has_error_label ("Retryable" )
2821
+ overloaded = exc_to_check .has_error_label ("SystemOverloaded" )
2824
2822
if not self ._retryable and not always_retryable :
2825
2823
raise
2826
2824
if retryable_write_label or always_retryable :
@@ -2829,7 +2827,7 @@ async def run(self) -> T:
2829
2827
if not always_retryable and (
2830
2828
not retryable_write_label or self ._is_not_eligible_for_retry ()
2831
2829
):
2832
- if exc .has_error_label ("NoWritesPerformed" ) and self ._last_error :
2830
+ if exc_to_check .has_error_label ("NoWritesPerformed" ) and self ._last_error :
2833
2831
raise self ._last_error from exc
2834
2832
else :
2835
2833
raise
@@ -2838,7 +2836,7 @@ async def run(self) -> T:
2838
2836
self ._bulk .retrying = True
2839
2837
else :
2840
2838
self ._retrying = True
2841
- if not exc .has_error_label ("NoWritesPerformed" ):
2839
+ if not exc_to_check .has_error_label ("NoWritesPerformed" ):
2842
2840
self ._last_error = exc
2843
2841
if self ._last_error is None :
2844
2842
self ._last_error = exc
@@ -2849,7 +2847,7 @@ async def run(self) -> T:
2849
2847
self ._always_retryable = always_retryable
2850
2848
if always_retryable :
2851
2849
if self ._attempt_number > _MAX_RETRIES :
2852
- if exc .has_error_label ("NoWritesPerformed" ) and self ._last_error :
2850
+ if exc_to_check .has_error_label ("NoWritesPerformed" ) and self ._last_error :
2853
2851
raise self ._last_error from exc
2854
2852
else :
2855
2853
raise
0 commit comments