40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
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
|