# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Log filters for Django loggers."""

import logging
from typing import override

from django.db.utils import OperationalError


class SuppressDatabaseErrors(logging.Filter):
    """Suppresses database errors from the logs."""

    @override
    def filter(self, record: logging.LogRecord) -> bool:
        """Filter out database errors from the logs."""
        if (
            record.exc_info is not None
            and record.exc_info[0] is not None
            and issubclass(record.exc_info[0], OperationalError)
        ):
            return False
        return True
