-
Notifications
You must be signed in to change notification settings - Fork 12
/
hacks.py
28 lines (25 loc) · 1.03 KB
/
hacks.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
from logging import Logger, getLogger
from typing import Callable
def noexcept(log: Logger = getLogger(__name__)):
"""
Grab all uncaught exceptions and log them with the logger
of the module implementing the decorated function.
:param log: logger of module using this.
:return: False
"""
def noexcept_decorator(f: Callable):
def noexcept_inner(*args, **kwargs):
# noinspection PyBroadException
try:
return f(*args, **kwargs)
except:
log.exception("An unexpected error occurred.")
return False
return noexcept_inner
if not isinstance(log, Logger) and callable(log):
# probably was called without arguments
actual_log = getLogger(__name__)
actual_log.warning("\033[1;33mSome monkey used the noexcept decorator without arguments, "
"which should not have happened. Go bug the devs about it.\033[1;0m")
return noexcept_decorator(log)
return noexcept_decorator