Created
March 5, 2022 13:36
-
-
Save Winand/0af3b96072d22361f591ab5d047b683b to your computer and use it in GitHub Desktop.
Пример обработки исключений в запросах requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_timeout_handler(func, url, timeout, retries=None, params={}, **kw): | |
"Retries on read timeout" | |
if not hasattr(timeout, '__iter__'): | |
timeout = [timeout] * (retries or 1) | |
elif retries is not None: | |
raise ValueError("retries specified along with list of timeouts") | |
for retry, tm in enumerate(timeout): | |
try: | |
r = None # init | |
r = func(url, params=params, timeout=tm, **kw) | |
if retry > 0: | |
logging.warning("#Success (%s), %d retry(s)", params, retry) | |
break # exit for..in | |
except requests.exceptions.ReadTimeout as e: # FIXME: TEST | |
logging.warning('READ TIMEOUT %s', e.args) # args -> urllib3.exceptions.ReadTimeoutError | |
logging.warning( | |
"#Request (%s) failed (timeout %ds, code %d), retry", | |
params, tm, getattr(r, 'status_code', 0) | |
) | |
except requests.exceptions.ConnectionError as e: | |
# https://github.com/requests/requests/issues/2392 | |
# ProtocolError=Удаленный хост принудительно разорвал существующее подключение | |
if not isinstance(e.args[0], (#urllib3.exceptions.ReadTimeoutError, #<-TimeoutError | |
urllib3.exceptions.ProtocolError, | |
urllib3.exceptions.TimeoutError, | |
urllib3.exceptions.MaxRetryError, | |
)): | |
logging.warning("DEBUG: %s %s", type(e.args[0]), e.args[0]) | |
raise # it's not a timeout error, re-raise | |
logging.warning( | |
"#Request (%s) failed (timeout %ds, code %d), retry", | |
params, tm, getattr(r, 'status_code', 0) | |
) | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment