utils: improve logging

This commit is contained in:
dece 2022-11-29 12:59:05 +01:00
parent c8c54f2e37
commit 086ec48823
2 changed files with 13 additions and 7 deletions

View file

@ -126,7 +126,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
except KeyboardInterrupt:
self.log_i("Caught keyboard interrupt.")
except Exception as exc:
self.log_c(f"Caught unhandled {type(exc)}: {exc}")
self.log_c(f"Caught unhandled {type(exc).__name__}: {exc}")
_, _, exc_traceback = sys.exc_info()
for line in traceback.format_tb(exc_traceback):
self.log_d(line.rstrip())

View file

@ -1,14 +1,20 @@
import random
from typing import Optional
from typing import Callable, Optional
import requests
def http_get(url: str) -> Optional[str]:
def http_get(url: str, log_e: Optional[Callable] = None) -> Optional[str]:
"""Get the HTML as text from this URL.
log_e is an optional error logging function."""
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
response.raise_for_status()
except OSError as exc:
if log_e:
log_e(f"http_get error: {exc}")
return None
return response.text
def proc(proba_percentage: int) -> bool: