41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import logging
|
|
from typing import Literal, NoReturn, Type, Union
|
|
|
|
logger = logging.getLogger('discord_app')
|
|
|
|
|
|
def log_errors(func):
|
|
"""
|
|
This wrapper function will force all exceptions to be logged with execution and stack info.
|
|
"""
|
|
|
|
def wrap(*args, **kwargs):
|
|
try:
|
|
result = func(*args, **kwargs)
|
|
except Exception as e:
|
|
logger.error(func.__name__)
|
|
log_exception(e)
|
|
return result # type: ignore
|
|
|
|
return wrap
|
|
|
|
def log_exception(e: Union[Exception, Type[Exception]], msg: str = '', level: Literal['debug', 'error', 'info', 'warn'] = 'error') -> NoReturn:
|
|
if level == 'debug':
|
|
logger.debug(msg, exc_info=True, stack_info=True)
|
|
elif level == 'error':
|
|
logger.error(msg, exc_info=True, stack_info=True)
|
|
elif level == 'info':
|
|
logger.info(msg, exc_info=True, stack_info=True)
|
|
else:
|
|
logger.warning(msg, exc_info=True, stack_info=True)
|
|
|
|
# Check if 'e' is an exception class or instance
|
|
if isinstance(e, Exception):
|
|
raise e # If 'e' is already an instance of an exception
|
|
else:
|
|
raise e(msg) # If 'e' is an exception class
|
|
return
|
|
|
|
|
|
class ApiException(Exception):
|
|
pass |