Code: Select all
from logging import getLogger
from os.path import basename
from traceback import extract_tb
logSys = getLogger()
def catchUnexpectedError(funct):
def wrapper():
try:
funct()
except Exception as unknown:
tb = extract_tb(unknown.__traceback__)[0]
exc, msg = repr(unknown).rstrip(')').split('(', maxsplit=1)
logSys.critical(f'Execution finished by exception {exc}: {msg.strip(chr(34))}.')
logSys.critical(f'Exception caused by: {tb.line} (line {tb.lineno}) at {basename(tb.filename)}')
return wrapper
Code: Select all
@catchUnexpectedError
def hello(name):
print(f'Good morning {name}!')
hello()
Code: Select all
[CRITICAL] Execution finished by exception TypeError: hello() missing 1 required positional argument: 'name'. (15:49:26 28/10/2025)
[CRITICAL] Exception caused by: funct() (line 51) at __init__.py (15:49:26 28/10/2025)
 Mobile version
 Mobile version