feat: Add detailed log handling options for Cognee exceptions [COG-1983] (#782)

<!-- .github/pull_request_template.md -->

## Description
Add log handling options for cognee exceptions

## 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.
This commit is contained in:
Igor Ilic 2025-04-28 22:46:18 +02:00 committed by GitHub
parent c4915a4136
commit 773752a4be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 6 deletions

View file

@ -12,13 +12,22 @@ class CogneeApiError(Exception):
message: str = "Service is unavailable.", message: str = "Service is unavailable.",
name: str = "Cognee", name: str = "Cognee",
status_code=status.HTTP_418_IM_A_TEAPOT, status_code=status.HTTP_418_IM_A_TEAPOT,
log=True,
log_level="ERROR",
): ):
self.message = message self.message = message
self.name = name self.name = name
self.status_code = status_code self.status_code = status_code
# Automatically log the exception details # Automatically log the exception details
logger.error(f"{self.name}: {self.message} (Status code: {self.status_code})") if log and (log_level == "ERROR"):
logger.error(f"{self.name}: {self.message} (Status code: {self.status_code})")
elif log and (log_level == "WARNING"):
logger.warning(f"{self.name}: {self.message} (Status code: {self.status_code})")
elif log and (log_level == "INFO"):
logger.info(f"{self.name}: {self.message} (Status code: {self.status_code})")
elif log and (log_level == "DEBUG"):
logger.debug(f"{self.name}: {self.message} (Status code: {self.status_code})")
super().__init__(self.message, self.name) super().__init__(self.message, self.name)

View file

@ -58,7 +58,7 @@ def record_graph_changes(func):
session.add(relationship) session.add(relationship)
await session.flush() await session.flush()
except Exception as e: except Exception as e:
logger.error(f"Error adding relationship: {e}") logger.debug(f"Error adding relationship: {e}")
await session.rollback() await session.rollback()
continue continue
@ -78,14 +78,14 @@ def record_graph_changes(func):
session.add(relationship) session.add(relationship)
await session.flush() await session.flush()
except Exception as e: except Exception as e:
logger.error(f"Error adding relationship: {e}") logger.debug(f"Error adding relationship: {e}")
await session.rollback() await session.rollback()
continue continue
try: try:
await session.commit() await session.commit()
except Exception as e: except Exception as e:
logger.error(f"Error committing session: {e}") logger.debug(f"Error committing session: {e}")
return result return result

View file

@ -8,5 +8,7 @@ class CollectionNotFoundError(CriticalError):
message, message,
name: str = "CollectionNotFoundError", name: str = "CollectionNotFoundError",
status_code: int = status.HTTP_422_UNPROCESSABLE_ENTITY, status_code: int = status.HTTP_422_UNPROCESSABLE_ENTITY,
log=True,
log_level="ERROR",
): ):
super().__init__(message, name, status_code) super().__init__(message, name, status_code, log, log_level)

View file

@ -184,7 +184,9 @@ class PGVectorAdapter(SQLAlchemyAdapter, VectorDBInterface):
if collection_name in metadata.tables: if collection_name in metadata.tables:
return metadata.tables[collection_name] return metadata.tables[collection_name]
else: else:
raise CollectionNotFoundError(f"Collection '{collection_name}' not found!") raise CollectionNotFoundError(
f"Collection '{collection_name}' not found!", log_level="DEBUG"
)
async def retrieve(self, collection_name: str, data_point_ids: List[str]): async def retrieve(self, collection_name: str, data_point_ids: List[str]):
# Get PGVectorDataPoint Table from database # Get PGVectorDataPoint Table from database
@ -266,6 +268,7 @@ class PGVectorAdapter(SQLAlchemyAdapter, VectorDBInterface):
# Get PGVectorDataPoint Table from database # Get PGVectorDataPoint Table from database
PGVectorDataPoint = await self.get_table(collection_name) PGVectorDataPoint = await self.get_table(collection_name)
# NOTE: This needs to be initialized in case search doesn't return a value
closest_items = [] closest_items = []
# Use async session to connect to the database # Use async session to connect to the database