@@ -270,22 +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 Exception as e :
287- raise e
278+ and e .status_code != 429
279+ )
280+ if isinstance (e , requests .exceptions .RequestException ):
281+ return (
282+ e .response is not None
283+ and e .response .status_code < 500
284+ and e .response .status_code != 429
285+ )
286+ return False
288287
289- 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 )
290297
291298 return execute_task_with_backoff ()
0 commit comments