cognee/cognee/infrastructure/databases/exceptions/exceptions.py
Daulet Amirkhanov 056424f244
feat: fs-cache (#1645)
<!-- .github/pull_request_template.md -->

## Description
<!--
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->

Implement File-Based Version of the Redis Cache Adapter

Description and acceptance criteria:

This PR introduces a file-based cache adapter as an alternative to the
existing Redis-based adapter. It provides the same core functionality
for caching session data and maintaining context across multiple user
interactions but stores data locally in files instead of Redis.

Because the shared Kùzu lock mechanism relies on Redis, it is not
supported in this implementation. If a lock is configured, the adapter
will raise an error to prevent misconfiguration.

You can test this adapter by enabling caching with the following
settings:

caching=True
cache_backend="fs"

When running multiple searches in a session, the system should correctly
maintain conversational context. For example:

- What is XY?
- Are you sure?
- What was my first question?

In this case, the adapter should preserve previous user–Cognee
interactions within the cache file so that follow-up queries remain
context-aware.


## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [x] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):

## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->

## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [x] **I have tested my changes thoroughly before submitting this PR**
- [x] **This PR contains minimal changes necessary to address the
issue/feature**
- [x] My code follows the project's coding standards and style
guidelines
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] I have added necessary documentation (if applicable)
- [x] All new and existing tests pass
- [x] I have searched existing PRs to ensure this change hasn't been
submitted already
- [x] I have linked any relevant issues in the description
- [x] My commits have clear and descriptive messages

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.

---------

Co-authored-by: Vasilije <8619304+Vasilije1990@users.noreply.github.com>
Co-authored-by: hajdul88 <52442977+hajdul88@users.noreply.github.com>
2025-11-12 15:34:30 +01:00

166 lines
5.6 KiB
Python

from fastapi import status
from cognee.exceptions import CogneeSystemError, CogneeValidationError, CogneeConfigurationError
class DatabaseNotCreatedError(CogneeSystemError):
"""
Represents an error indicating that the database has not been created. This error should
be raised when an attempt is made to access the database before it has been initialized.
Inherits from CogneeSystemError. Overrides the constructor to include a default message and
status code.
"""
def __init__(
self,
message: str = "The database has not been created yet. Please call `await setup()` first.",
name: str = "DatabaseNotCreatedError",
status_code: int = status.HTTP_422_UNPROCESSABLE_CONTENT,
):
super().__init__(message, name, status_code)
class EntityNotFoundError(CogneeValidationError):
"""
Represents an error when a requested entity is not found in the database. This class
inherits from CogneeValidationError.
Public methods:
- __init__ : Initializes the EntityNotFoundError with a specific message, name, and
status code.
Instance variables:
- message: A string containing the error message.
- name: A string representing the name of the error type.
- status_code: An integer indicating the HTTP status code associated with the error.
"""
def __init__(
self,
message: str = "The requested entity does not exist.",
name: str = "EntityNotFoundError",
status_code=status.HTTP_404_NOT_FOUND,
):
self.message = message
self.name = name
self.status_code = status_code
# super().__init__(message, name, status_code) :TODO: This is not an error anymore with the dynamic exception handling therefore we shouldn't log error
class EntityAlreadyExistsError(CogneeValidationError):
"""
Represents an error when an entity creation is attempted but the entity already exists.
This class is derived from CogneeValidationError and is used to signal a conflict in operations
involving resource creation.
"""
def __init__(
self,
message: str = "The entity already exists.",
name: str = "EntityAlreadyExistsError",
status_code=status.HTTP_409_CONFLICT,
):
super().__init__(message, name, status_code)
class NodesetFilterNotSupportedError(CogneeConfigurationError):
"""
Raise an exception when a nodeset filter is not supported by the current database.
This exception inherits from `CogneeConfigurationError` and is designed to provide information
about the specific issue of unsupported nodeset filters in the context of graph
databases.
"""
def __init__(
self,
message: str = "The nodeset filter is not supported in the current graph database.",
name: str = "NodeSetFilterNotSupportedError",
status_code=status.HTTP_404_NOT_FOUND,
):
self.message = message
self.name = name
self.status_code = status_code
class EmbeddingException(CogneeConfigurationError):
"""
Custom exception for handling embedding-related errors.
This exception class is designed to indicate issues specifically related to embeddings
within the application. It extends the base exception class CogneeConfigurationError allows
for customization of the error message, name, and status code.
"""
def __init__(
self,
message: str = "Embedding Exception.",
name: str = "EmbeddingException",
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
):
super().__init__(message, name, status_code)
class MissingQueryParameterError(CogneeValidationError):
"""
Raised when neither 'query_text' nor 'query_vector' is provided,
and at least one is required to perform the operation.
"""
def __init__(
self,
name: str = "MissingQueryParameterError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
message = "One of query_text or query_vector must be provided!"
super().__init__(message, name, status_code)
class MutuallyExclusiveQueryParametersError(CogneeValidationError):
"""
Raised when both 'text' and 'embedding' are provided to the search function,
but only one type of input is allowed at a time.
"""
def __init__(
self,
name: str = "MutuallyExclusiveQueryParametersError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
message = "The search function accepts either text or embedding as input, but not both."
super().__init__(message, name, status_code)
class CacheConnectionError(CogneeConfigurationError):
"""
Raised when connection to the cache database (e.g., Redis) fails.
This error indicates that the cache service is unavailable or misconfigured.
"""
def __init__(
self,
message: str = "Failed to connect to cache database. Please check your cache configuration.",
name: str = "CacheConnectionError",
status_code: int = status.HTTP_503_SERVICE_UNAVAILABLE,
):
super().__init__(message, name, status_code)
class SharedKuzuLockRequiresRedisError(CogneeConfigurationError):
"""
Raised when shared Kuzu locking is requested without configuring the Redis backend.
"""
def __init__(
self,
message: str = (
"Shared Kuzu lock requires Redis cache backend. Configure Redis to enable shared Kuzu locking."
),
name: str = "SharedKuzuLockRequiresRedisError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
super().__init__(message, name, status_code)