/
nolizard
/
DQA
Обзор
Документация
Войти
/
nolizard
/
DQA
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
dqa/exceptions.py
55 строк
2 KB
Mikhail Nyrkov
Initial commit
14 апр 2026, 19:01
14 апр 2026, 19:01
8911266
Код
Авторство
О чём код?
"""Exceptions Handler module.""" from typing import Any, Callable, Tuple from functools import wraps from beartype import beartype def except_handler(func: Callable[..., Any]) -> Callable[..., Any]: ''' A decorator that checks the types of input arguments, catches errors that occur inside the decorated function. :param func: decorable function :type func: Callable[..., Any] :return: wrapper :rtype: Callable[..., Any] ''' @wraps(func) def wrapper(*args, **kwargs): try: beartype(func)(*args, **kwargs) return func(*args, **kwargs) except Exception as exception: except_message = gen_except_message(func, exception, *args) print(except_message) return wrapper def gen_except_message(func: Callable[..., Any], exception: Exception, *args: Tuple[Any, ...]) -> str: ''' Generated error message. :param func: decorable function :type func: Callable[..., Any] :param exception: object containing the received error :type exception: Exception :param *args: arguments of decorable function :type *args: Tuple[Any, ...] :return: generated error message :rtype: str ''' if args and hasattr(args[0], '__class__'): class_name = args[0].__class__.__name__ if class_name: return (f"Произошла ошибка в функции '{func.__name__}' "+ f"класса '{class_name}' из модуля '{func.__module__}' "+ f"Тип ошибки: '{type(exception).__name__}', Сообщение об ошибке: {exception}") else: return (f"Произошла ошибка в функции '{func.__name__}' "+ f"из модуля '{func.__module__}' "+ f"Тип ошибки: '{type(exception).__name__}', Сообщение об ошибке: {exception}")