@@ -270,29 +270,29 @@ def _process_upload_media_job(
270270 def _request_with_backoff (
271271 self , func : Callable [P , T ], * args : P .args , ** kwargs : P .kwargs
272272 ) -> T :
273- @backoff .on_exception (
274- backoff .expo , Exception , max_tries = self ._max_retries , logger = None
275- )
276- def execute_task_with_backoff () -> T :
277- try :
278- return func (* args , ** kwargs )
279- except ApiError as e :
280- if (
273+ def _should_give_up (e : Exception ) -> bool :
274+ if isinstance (e , ApiError ):
275+ return (
281276 e .status_code is not None
282277 and 400 <= e .status_code < 500
283- and (e .status_code ) != 429
284- ):
285- raise e
286- except requests .exceptions .RequestException as e :
287- if (
278+ and e .status_code != 429
279+ )
280+ if isinstance (e , requests .exceptions .RequestException ):
281+ return (
288282 e .response is not None
289- and hasattr (e .response , "status_code" )
290- and (e .response .status_code >= 500 or e .response .status_code == 429 )
291- ):
292- raise
293-
294- raise e # break retries for all other status codes
283+ and e .response .status_code < 500
284+ and e .response .status_code != 429
285+ )
286+ return False
295287
296- raise Exception ("Failed to execute task" )
288+ @backoff .on_exception (
289+ backoff .expo ,
290+ Exception ,
291+ max_tries = self ._max_retries ,
292+ giveup = _should_give_up ,
293+ logger = None ,
294+ )
295+ def execute_task_with_backoff () -> T :
296+ return func (* args , ** kwargs )
297297
298298 return execute_task_with_backoff ()
0 commit comments