mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-25 15:24:17 +00:00
Use logging in utils too
This commit is contained in:
parent
641c2736bd
commit
013ffc8bbc
1 changed files with 21 additions and 15 deletions
30
utils.py
30
utils.py
|
@ -6,6 +6,12 @@ import typing
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import importlib
|
import importlib
|
||||||
|
import logging
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
language = config["Config"]["language"]
|
language = config["Config"]["language"]
|
||||||
try:
|
try:
|
||||||
|
@ -128,38 +134,38 @@ def catch_telegram_errors(func):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
# Bot was blocked by the user
|
# Bot was blocked by the user
|
||||||
except telegram.error.Unauthorized:
|
except telegram.error.Unauthorized:
|
||||||
print(f"Unauthorized to call {func.__name__}(), skipping.")
|
log.debug(f"Unauthorized to call {func.__name__}(), skipping.")
|
||||||
break
|
break
|
||||||
# Telegram API didn't answer in time
|
# Telegram API didn't answer in time
|
||||||
except telegram.error.TimedOut:
|
except telegram.error.TimedOut:
|
||||||
print(f"Timed out while calling {func.__name__}(),"
|
log.warning(f"Timed out while calling {func.__name__}(),"
|
||||||
f" retrying in {config['Telegram']['timed_out_pause']} secs...")
|
f" retrying in {config['Telegram']['timed_out_pause']} secs...")
|
||||||
time.sleep(int(config["Telegram"]["timed_out_pause"]))
|
time.sleep(int(config["Telegram"]["timed_out_pause"]))
|
||||||
# Telegram is not reachable
|
# Telegram is not reachable
|
||||||
except telegram.error.NetworkError as error:
|
except telegram.error.NetworkError as error:
|
||||||
print(f"Network error while calling {func.__name__}(),"
|
log.error(f"Network error while calling {func.__name__}(),"
|
||||||
f" retrying in {config['Telegram']['error_pause']} secs...")
|
f" retrying in {config['Telegram']['error_pause']} secs...\n"
|
||||||
# Display the full NetworkError
|
f"Full error: {error.message}")
|
||||||
print(f"Full error: {error.message}")
|
|
||||||
time.sleep(int(config["Telegram"]["error_pause"]))
|
time.sleep(int(config["Telegram"]["error_pause"]))
|
||||||
# Unknown error
|
# Unknown error
|
||||||
except telegram.error.TelegramError as error:
|
except telegram.error.TelegramError as error:
|
||||||
if error.message.lower() in ["bad gateway", "invalid server response"]:
|
if error.message.lower() in ["bad gateway", "invalid server response"]:
|
||||||
print(f"Bad Gateway while calling {func.__name__}(),"
|
log.warning(f"Bad Gateway while calling {func.__name__}(),"
|
||||||
f" retrying in {config['Telegram']['error_pause']} secs...")
|
f" retrying in {config['Telegram']['error_pause']} secs...")
|
||||||
time.sleep(int(config["Telegram"]["error_pause"]))
|
time.sleep(int(config["Telegram"]["error_pause"]))
|
||||||
elif error.message.lower() == "timed out":
|
elif error.message.lower() == "timed out":
|
||||||
print(f"Timed out while calling {func.__name__}(),"
|
log.warning(f"Timed out while calling {func.__name__}(),"
|
||||||
f" retrying in {config['Telegram']['timed_out_pause']} secs...")
|
f" retrying in {config['Telegram']['timed_out_pause']} secs...")
|
||||||
time.sleep(int(config["Telegram"]["timed_out_pause"]))
|
time.sleep(int(config["Telegram"]["timed_out_pause"]))
|
||||||
else:
|
else:
|
||||||
print(f"Telegram error while calling {func.__name__}(),"
|
log.error(f"Telegram error while calling {func.__name__}(),"
|
||||||
f" retrying in {config['Telegram']['error_pause']} secs...")
|
f" retrying in {config['Telegram']['error_pause']} secs...\n"
|
||||||
# Display the full TelegramError
|
f"Full error: {error.message}")
|
||||||
print(f"Full error: {error.message}")
|
|
||||||
# Send the error to the Sentry server
|
# Send the error to the Sentry server
|
||||||
if sentry_client is not None:
|
if sentry_client is not None:
|
||||||
sentry_client.captureException(exc_info=sys.exc_info())
|
sentry_client.captureException(exc_info=sys.exc_info())
|
||||||
|
else:
|
||||||
|
traceback.print_exception(*sys.exc_info())
|
||||||
time.sleep(int(config["Telegram"]["error_pause"]))
|
time.sleep(int(config["Telegram"]["error_pause"]))
|
||||||
|
|
||||||
return result_func
|
return result_func
|
||||||
|
|
Loading…
Reference in a new issue