-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecorators.py
30 lines (26 loc) · 1.19 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import logging
from rocketapi.exceptions import NotFoundException, BadResponseException
logging.basicConfig(level=logging.INFO)
def retry_on_exception(max_tries=3):
def decorator(func):
def wrapper(*args, **kwargs):
for i in range(max_tries):
try:
return func(*args, **kwargs)
except (NotFoundException, BadResponseException):
if i < max_tries - 1:
logging.warning(f"Attempt {i + 1} of {max_tries} failed. Retrying...")
continue
else:
logging.error(f"Attempt {i + 1} of {max_tries} failed. Aborting...")
return None
except Exception as e:
logging.warning(f"An unexpected error occurred: {e}")
if i < max_tries - 1:
logging.warning(f"Attempt {i + 1} of {max_tries} failed. Retrying...")
continue
else:
logging.error(f"Attempt {i + 1} of {max_tries} failed. Aborting...")
return None
return wrapper
return decorator