feat: перенос логики 3 лабы
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
from collections.abc import Callable
|
||||
from functools import wraps
|
||||
from typing import Any
|
||||
|
||||
import asyncpg
|
||||
|
||||
from config import LOGGER
|
||||
from domain.errors import UnavailableError
|
||||
|
||||
DATABASE_UNAVAILABLE_ERRORS: tuple[type[Exception], ...] = (
|
||||
OSError,
|
||||
TimeoutError,
|
||||
asyncpg.PostgresConnectionError,
|
||||
asyncpg.InterfaceError,
|
||||
asyncpg.exceptions.OperatorInterventionError,
|
||||
asyncpg.exceptions.InsufficientResourcesError,
|
||||
)
|
||||
|
||||
|
||||
def repository_logs(func: Callable) -> Callable:
|
||||
"""
|
||||
Decorator for logging database requests.
|
||||
"""
|
||||
|
||||
@wraps(func)
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
log_text = (
|
||||
f"Request to DATABASE || Object: {args[0].__class__.__name__} "
|
||||
f"|| Function: {func.__name__} || "
|
||||
)
|
||||
try:
|
||||
result = await func(*args, **kwargs)
|
||||
except DATABASE_UNAVAILABLE_ERRORS as error:
|
||||
LOGGER.warning(log_text + f"failed || Error: {type(error).__name__}")
|
||||
raise UnavailableError() from None
|
||||
LOGGER.debug(log_text + "completed")
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user