From bed523a36b18b8b6ac76de69f344973baf5cd043 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:59:12 +0200 Subject: [PATCH] feat: api error handling restruct --- cognee/api/v1/config/config.py | 12 ++----- cognee/api/v1/delete/delete.py | 2 +- cognee/api/v1/delete/exceptions.py | 50 -------------------------- cognee/api/v1/exceptions/__init__.py | 6 +++- cognee/api/v1/exceptions/exceptions.py | 37 +++++++++++++++++++ 5 files changed, 46 insertions(+), 61 deletions(-) delete mode 100644 cognee/api/v1/delete/exceptions.py diff --git a/cognee/api/v1/config/config.py b/cognee/api/v1/config/config.py index 03df700cb..464753438 100644 --- a/cognee/api/v1/config/config.py +++ b/cognee/api/v1/config/config.py @@ -129,9 +129,7 @@ class config: if hasattr(relational_db_config, key): object.__setattr__(relational_db_config, key, value) else: - raise InvalidAttributeError( - message=f"'{key}' is not a valid attribute of the config." - ) + raise InvalidConfigAttributeError(attribute=key) @staticmethod def set_migration_db_config(config_dict: dict): @@ -143,9 +141,7 @@ class config: if hasattr(migration_db_config, key): object.__setattr__(migration_db_config, key, value) else: - raise InvalidAttributeError( - message=f"'{key}' is not a valid attribute of the config." - ) + raise InvalidConfigAttributeError(attribute=key) @staticmethod def set_graph_db_config(config_dict: dict) -> None: @@ -169,9 +165,7 @@ class config: if hasattr(vector_db_config, key): object.__setattr__(vector_db_config, key, value) else: - raise InvalidAttributeError( - message=f"'{key}' is not a valid attribute of the config." - ) + InvalidConfigAttributeError(attribute=key) @staticmethod def set_vector_db_key(db_key: str): diff --git a/cognee/api/v1/delete/delete.py b/cognee/api/v1/delete/delete.py index 98f6cb9fc..73f264670 100644 --- a/cognee/api/v1/delete/delete.py +++ b/cognee/api/v1/delete/delete.py @@ -16,7 +16,7 @@ from cognee.modules.users.methods import get_default_user from cognee.modules.data.methods import get_authorized_existing_datasets from cognee.context_global_variables import set_database_global_context_variables -from cognee.api.v1.delete.exceptions import ( +from cognee.api.v1.exceptions import ( DocumentNotFoundError, DatasetNotFoundError, DocumentSubgraphNotFoundError, diff --git a/cognee/api/v1/delete/exceptions.py b/cognee/api/v1/delete/exceptions.py deleted file mode 100644 index a4d2a77ed..000000000 --- a/cognee/api/v1/delete/exceptions.py +++ /dev/null @@ -1,50 +0,0 @@ -from cognee.exceptions import CogneeApiError -from fastapi import status - - -class DocumentNotFoundError(CogneeApiError): - """Raised when a document cannot be found in the database.""" - - def __init__( - self, - message: str = "Document not found in database.", - name: str = "DocumentNotFoundError", - status_code: int = status.HTTP_404_NOT_FOUND, - ): - super().__init__(message, name, status_code) - - -class DatasetNotFoundError(CogneeApiError): - """Raised when a dataset cannot be found.""" - - def __init__( - self, - message: str = "Dataset not found.", - name: str = "DatasetNotFoundError", - status_code: int = status.HTTP_404_NOT_FOUND, - ): - super().__init__(message, name, status_code) - - -class DataNotFoundError(CogneeApiError): - """Raised when a dataset cannot be found.""" - - def __init__( - self, - message: str = "Data not found.", - name: str = "DataNotFoundError", - status_code: int = status.HTTP_404_NOT_FOUND, - ): - super().__init__(message, name, status_code) - - -class DocumentSubgraphNotFoundError(CogneeApiError): - """Raised when a document's subgraph cannot be found in the graph database.""" - - def __init__( - self, - message: str = "Document subgraph not found in graph database.", - name: str = "DocumentSubgraphNotFoundError", - status_code: int = status.HTTP_404_NOT_FOUND, - ): - super().__init__(message, name, status_code) diff --git a/cognee/api/v1/exceptions/__init__.py b/cognee/api/v1/exceptions/__init__.py index f18b9f99a..b2948934c 100644 --- a/cognee/api/v1/exceptions/__init__.py +++ b/cognee/api/v1/exceptions/__init__.py @@ -5,5 +5,9 @@ This module defines a set of exceptions for handling various data errors """ from .exceptions import ( - InvalidConfigAttributeError + InvalidConfigAttributeError, + DocumentNotFoundError, + DatasetNotFoundError, + DataNotFoundError, + DocumentSubgraphNotFoundError ) diff --git a/cognee/api/v1/exceptions/exceptions.py b/cognee/api/v1/exceptions/exceptions.py index 9875d179b..8b65691f1 100644 --- a/cognee/api/v1/exceptions/exceptions.py +++ b/cognee/api/v1/exceptions/exceptions.py @@ -1,5 +1,6 @@ from cognee.exceptions import ( CogneeConfigurationError, + CogneeValidationError ) from fastapi import status @@ -13,3 +14,39 @@ class InvalidConfigAttributeError(CogneeConfigurationError): ): message = f"'{attribute}' is not a valid attribute of the configuration." super().__init__(message, name, status_code) + +class DocumentNotFoundError(CogneeValidationError): + def __init__( + self, + message: str = "Document not found in database.", + name: str = "DocumentNotFoundError", + status_code: int = status.HTTP_404_NOT_FOUND, + ): + super().__init__(message, name, status_code) + +class DatasetNotFoundError(CogneeValidationError): + def __init__( + self, + message: str = "Dataset not found.", + name: str = "DatasetNotFoundError", + status_code: int = status.HTTP_404_NOT_FOUND, + ): + super().__init__(message, name, status_code) + +class DataNotFoundError(CogneeValidationError): + def __init__( + self, + message: str = "Data not found.", + name: str = "DataNotFoundError", + status_code: int = status.HTTP_404_NOT_FOUND, + ): + super().__init__(message, name, status_code) + +class DocumentSubgraphNotFoundError(CogneeValidationError): + def __init__( + self, + message: str = "Document subgraph not found in graph database.", + name: str = "DocumentSubgraphNotFoundError", + status_code: int = status.HTTP_404_NOT_FOUND, + ): + super().__init__(message, name, status_code)