From 6f0e00d5177e625874c50abfca4848a1b76eb914 Mon Sep 17 00:00:00 2001 From: samschott Date: Fri, 3 Jun 2022 23:57:24 +0200 Subject: [PATCH] [errorhandling] allow retrying with error message specificity --- src/maestral/errorhandling.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/maestral/errorhandling.py b/src/maestral/errorhandling.py index c81b77e9..60c5c079 100644 --- a/src/maestral/errorhandling.py +++ b/src/maestral/errorhandling.py @@ -7,6 +7,7 @@ from __future__ import annotations # system imports import os +import re import time import errno import contextlib @@ -823,13 +824,18 @@ def get_bad_path_error_msg( def retry_on_error( - error_cls: type[Exception], max_retries: int, backoff: int = 0 + error_cls: type[Exception], + max_retries: int, + backoff: int = 0, + msg_regex: str | None = None, ) -> Callable[[FT], FT]: """ A decorator to retry a function call if a specified exception occurs. :param error_cls: Error type to catch. :param max_retries: Maximum number of retries. + :param msg_regex: If provided, retry errors only if the regex matches the error + message. Matches are found with :meth:`re.search()`. :param backoff: Time in seconds to sleep before retry. """ @@ -843,6 +849,14 @@ def retry_on_error( return func(*args, **kwargs) except error_cls as exc: + if msg_regex is not None: + # Raise if there is no error message to match. + if len(exc.args[0]) == 0 or not isinstance(exc.args[0], str): + raise exc + # Raise if regex does not match message. + if not re.search(msg_regex, exc.args[0]): + raise exc + if tries < max_retries: tries += 1 if backoff > 0: