refactor: Split entity related exceptions into graph and database exceptions

Move and split database entity related exceptions into graph and database exceptions

Refactor COG-502
This commit is contained in:
Igor Ilic 2024-11-29 17:40:48 +01:00
parent eb09e5ad89
commit 6b97e95e14
11 changed files with 79 additions and 45 deletions

View file

@ -9,7 +9,7 @@ from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel
from cognee.api.DTO import OutDTO
from cognee.exceptions import EntityNotFoundError
from cognee.infrastructure.databases.exceptions import EntityNotFoundError
from cognee.modules.users.models import User
from cognee.modules.users.methods import get_authenticated_user
from cognee.modules.pipelines.models import PipelineRunStatus

View file

@ -8,9 +8,6 @@ such as service failures, resource conflicts, and invalid operations.
from .exceptions import (
CogneeApiError,
ServiceError,
EntityNotFoundError,
EntityAlreadyExistsError,
InvalidOperationError,
InvalidValueError,
InvalidAttributeError,
)

View file

@ -34,42 +34,6 @@ class ServiceError(CogneeApiError):
super().__init__(message, name, status_code)
class EntityNotFoundError(CogneeApiError):
"""Database returns nothing"""
def __init__(
self,
message: str = "The requested entity does not exist.",
name: str = "EntityNotFoundError",
status_code=status.HTTP_404_NOT_FOUND,
):
super().__init__(message, name, status_code)
class EntityAlreadyExistsError(CogneeApiError):
"""Conflict detected, like trying to create a resource that already exists"""
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 InvalidOperationError(CogneeApiError):
"""Invalid operations like trying to delete a non-existing entity, etc."""
def __init__(
self,
message: str = "Invalid operation attempted.",
name: str = "InvalidOperationError",
status_code=status.HTTP_400_BAD_REQUEST,
):
super().__init__(message, name, status_code)
class InvalidValueError(CogneeApiError):
def __init__(
self,
@ -79,6 +43,7 @@ class InvalidValueError(CogneeApiError):
):
super().__init__(message, name, status_code)
class InvalidAttributeError(CogneeApiError):
def __init__(
self,

View file

@ -0,0 +1,10 @@
"""
Custom exceptions for the Cognee API.
This module defines a set of exceptions for handling various database errors
"""
from .exceptions import (
EntityNotFoundError,
EntityAlreadyExistsError,
)

View file

@ -0,0 +1,25 @@
from cognee.exceptions import CogneeApiError
from fastapi import status
class EntityNotFoundError(CogneeApiError):
"""Database returns nothing"""
def __init__(
self,
message: str = "The requested entity does not exist.",
name: str = "EntityNotFoundError",
status_code=status.HTTP_404_NOT_FOUND,
):
super().__init__(message, name, status_code)
class EntityAlreadyExistsError(CogneeApiError):
"""Conflict detected, like trying to create a resource that already exists"""
def __init__(
self,
message: str = "The entity already exists.",
name: str = "EntityAlreadyExistsError",
status_code=status.HTTP_409_CONFLICT,
):
super().__init__(message, name, status_code)

View file

@ -7,7 +7,7 @@ from sqlalchemy import text, select, MetaData, Table
from sqlalchemy.orm import joinedload
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from cognee.exceptions import EntityNotFoundError
from cognee.infrastructure.databases.exceptions import EntityNotFoundError
from ..ModelBase import Base
class SQLAlchemyAdapter():

View file

@ -6,7 +6,8 @@ from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import JSON, Column, Table, select, delete
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from cognee.exceptions import EntityNotFoundError, InvalidValueError
from cognee.exceptions import InvalidValueError
from cognee.infrastructure.databases.exceptions import EntityNotFoundError
from cognee.infrastructure.engine import DataPoint
from .serialize_data import serialize_data

View file

@ -1,10 +1,11 @@
from typing import List, Dict, Union
from cognee.exceptions import EntityAlreadyExistsError, EntityNotFoundError, InvalidValueError
from cognee.exceptions import InvalidValueError
from cognee.modules.graph.exceptions import EntityNotFoundError, EntityAlreadyExistsError
from cognee.infrastructure.databases.graph.graph_db_interface import GraphDBInterface
from cognee.modules.graph.cognee_graph.CogneeGraphElements import Node, Edge
from cognee.modules.graph.cognee_graph.CogneeAbstractGraph import CogneeAbstractGraph
from cognee.infrastructure.databases.graph import get_graph_engine
class CogneeGraph(CogneeAbstractGraph):
"""

View file

@ -0,0 +1,10 @@
"""
Custom exceptions for the Cognee API.
This module defines a set of exceptions for handling various graph errors
"""
from .exceptions import (
EntityNotFoundError,
EntityAlreadyExistsError,
)

View file

@ -0,0 +1,25 @@
from cognee.exceptions import CogneeApiError
from fastapi import status
class EntityNotFoundError(CogneeApiError):
"""Database returns nothing"""
def __init__(
self,
message: str = "The requested entity does not exist.",
name: str = "EntityNotFoundError",
status_code=status.HTTP_404_NOT_FOUND,
):
super().__init__(message, name, status_code)
class EntityAlreadyExistsError(CogneeApiError):
"""Conflict detected, like trying to create a resource that already exists"""
def __init__(
self,
message: str = "The entity already exists.",
name: str = "EntityAlreadyExistsError",
status_code=status.HTTP_409_CONFLICT,
):
super().__init__(message, name, status_code)

View file

@ -11,7 +11,7 @@ import aiofiles
import pandas as pd
from pydantic import BaseModel
from cognee.exceptions import EntityNotFoundError
from cognee.modules.graph.exceptions import EntityNotFoundError, EntityAlreadyExistsError
from cognee.modules.ingestion.exceptions import IngestionError
from cognee.infrastructure.llm.prompts import read_query_prompt
from cognee.infrastructure.llm.get_llm_client import get_llm_client