feat: api error handling restruct

This commit is contained in:
hajdul88 2025-08-13 13:59:12 +02:00
parent 32996aa0d0
commit bed523a36b
5 changed files with 46 additions and 61 deletions

View file

@ -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):

View file

@ -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,

View file

@ -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)

View file

@ -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
)

View file

@ -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)