Improve exception handling to provide clearer error context
This commit is contained in:
parent
583923903c
commit
6acc44de16
12 changed files with 34 additions and 28 deletions
|
|
@ -70,11 +70,11 @@ After adding data, use `cognee cognify` to process it into knowledge graphs.
|
|||
await cognee.add(data=data_to_add, dataset_name=args.dataset_name)
|
||||
fmt.success(f"Successfully added data to dataset '{args.dataset_name}'")
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to add data: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to add data: {str(e)}") from e
|
||||
|
||||
asyncio.run(run_add())
|
||||
|
||||
except Exception as e:
|
||||
if isinstance(e, CliCommandInnerException):
|
||||
raise CliCommandException(str(e), error_code=1)
|
||||
raise CliCommandException(f"Error adding data: {str(e)}", error_code=1)
|
||||
raise CliCommandException(str(e), error_code=1) from e
|
||||
raise CliCommandException(f"Error adding data: {str(e)}", error_code=1) from e
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ After successful cognify processing, use `cognee search` to query the knowledge
|
|||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to cognify: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to cognify: {str(e)}") from e
|
||||
|
||||
result = asyncio.run(run_cognify())
|
||||
|
||||
|
|
@ -124,5 +124,5 @@ After successful cognify processing, use `cognee search` to query the knowledge
|
|||
|
||||
except Exception as e:
|
||||
if isinstance(e, CliCommandInnerException):
|
||||
raise CliCommandException(str(e), error_code=1)
|
||||
raise CliCommandException(f"Error during cognification: {str(e)}", error_code=1)
|
||||
raise CliCommandException(str(e), error_code=1) from e
|
||||
raise CliCommandException(f"Error during cognification: {str(e)}", error_code=1) from e
|
||||
|
|
|
|||
|
|
@ -79,8 +79,10 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
|
||||
except Exception as e:
|
||||
if isinstance(e, CliCommandInnerException):
|
||||
raise CliCommandException(str(e), error_code=1)
|
||||
raise CliCommandException(f"Error managing configuration: {str(e)}", error_code=1)
|
||||
raise CliCommandException(str(e), error_code=1) from e
|
||||
raise CliCommandException(
|
||||
f"Error managing configuration: {str(e)}", error_code=1
|
||||
) from e
|
||||
|
||||
def _handle_get(self, args: argparse.Namespace) -> None:
|
||||
try:
|
||||
|
|
@ -122,7 +124,7 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
fmt.note("Configuration viewing not fully implemented yet")
|
||||
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to get configuration: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to get configuration: {str(e)}") from e
|
||||
|
||||
def _handle_set(self, args: argparse.Namespace) -> None:
|
||||
try:
|
||||
|
|
@ -141,7 +143,7 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
fmt.error(f"Failed to set configuration key '{args.key}'")
|
||||
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to set configuration: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to set configuration: {str(e)}") from e
|
||||
|
||||
def _handle_unset(self, args: argparse.Namespace) -> None:
|
||||
try:
|
||||
|
|
@ -189,7 +191,7 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
fmt.note("Use 'cognee config list' to see all available configuration options")
|
||||
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to unset configuration: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to unset configuration: {str(e)}") from e
|
||||
|
||||
def _handle_list(self, args: argparse.Namespace) -> None:
|
||||
try:
|
||||
|
|
@ -209,7 +211,7 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
fmt.echo(" cognee config reset - Reset all to defaults")
|
||||
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to list configuration: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to list configuration: {str(e)}") from e
|
||||
|
||||
def _handle_reset(self, args: argparse.Namespace) -> None:
|
||||
try:
|
||||
|
|
@ -222,4 +224,4 @@ Configuration changes will affect how cognee processes and stores data.
|
|||
fmt.echo("This would reset all settings to their default values")
|
||||
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to reset configuration: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to reset configuration: {str(e)}") from e
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ Be careful with deletion operations as they are irreversible.
|
|||
else:
|
||||
await cognee.delete(dataset_name=args.dataset_name, user_id=args.user_id)
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to delete: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to delete: {str(e)}") from e
|
||||
|
||||
asyncio.run(run_delete())
|
||||
# This success message may be inaccurate due to the underlying bug, but we leave it for now.
|
||||
|
|
@ -108,5 +108,5 @@ Be careful with deletion operations as they are irreversible.
|
|||
|
||||
except Exception as e:
|
||||
if isinstance(e, CliCommandInnerException):
|
||||
raise CliCommandException(str(e), error_code=1)
|
||||
raise CliCommandException(f"Error deleting data: {str(e)}", error_code=1)
|
||||
raise CliCommandException(str(e), error_code=1) from e
|
||||
raise CliCommandException(f"Error deleting data: {str(e)}", error_code=1) from e
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ Search Types & Use Cases:
|
|||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise CliCommandInnerException(f"Failed to search: {str(e)}")
|
||||
raise CliCommandInnerException(f"Failed to search: {str(e)}") from e
|
||||
|
||||
results = asyncio.run(run_search())
|
||||
|
||||
|
|
@ -145,5 +145,5 @@ Search Types & Use Cases:
|
|||
|
||||
except Exception as e:
|
||||
if isinstance(e, CliCommandInnerException):
|
||||
raise CliCommandException(str(e), error_code=1)
|
||||
raise CliCommandException(f"Error searching: {str(e)}", error_code=1)
|
||||
raise CliCommandException(str(e), error_code=1) from e
|
||||
raise CliCommandException(f"Error searching: {str(e)}", error_code=1) from e
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ def parse_neptune_url(url: str) -> Tuple[str, str]:
|
|||
return graph_id, region
|
||||
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to parse Neptune Analytics URL '{url}': {str(e)}")
|
||||
raise ValueError(f"Failed to parse Neptune Analytics URL '{url}': {str(e)}") from e
|
||||
|
||||
|
||||
def validate_graph_id(graph_id: str) -> bool:
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@ class SQLAlchemyAdapter:
|
|||
try:
|
||||
data_entity = (await session.scalars(select(Data).where(Data.id == data_id))).one()
|
||||
except (ValueError, NoResultFound) as e:
|
||||
raise EntityNotFoundError(message=f"Entity not found: {str(e)}")
|
||||
raise EntityNotFoundError(message=f"Entity not found: {str(e)}") from e
|
||||
|
||||
# Check if other data objects point to the same raw data location
|
||||
raw_data_location_entities = (
|
||||
|
|
|
|||
|
|
@ -90,7 +90,9 @@ class FastembedEmbeddingEngine(EmbeddingEngine):
|
|||
|
||||
except Exception as error:
|
||||
logger.error(f"Embedding error in FastembedEmbeddingEngine: {str(error)}")
|
||||
raise EmbeddingException(f"Failed to index data points using model {self.model}")
|
||||
raise EmbeddingException(
|
||||
f"Failed to index data points using model {self.model}"
|
||||
) from error
|
||||
|
||||
def get_vector_size(self) -> int:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ class LiteLLMEmbeddingEngine(EmbeddingEngine):
|
|||
litellm.exceptions.NotFoundError,
|
||||
) as e:
|
||||
logger.error(f"Embedding error with model {self.model}: {str(e)}")
|
||||
raise EmbeddingException(f"Failed to index data points using model {self.model}")
|
||||
raise EmbeddingException(f"Failed to index data points using model {self.model}") from e
|
||||
|
||||
except Exception as error:
|
||||
logger.error("Error embedding text: %s", str(error))
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ async def get_authenticated_user(
|
|||
except Exception as e:
|
||||
# Convert any get_default_user failure into a proper HTTP 500 error
|
||||
logger.error(f"Failed to create default user: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to create default user: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to create default user: {str(e)}"
|
||||
) from e
|
||||
|
||||
return user
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@ async def create_role(
|
|||
# Add association directly to the association table
|
||||
role = Role(name=role_name, tenant_id=tenant.id)
|
||||
session.add(role)
|
||||
except IntegrityError:
|
||||
raise EntityAlreadyExistsError(message="Role already exists for tenant.")
|
||||
except IntegrityError as e:
|
||||
raise EntityAlreadyExistsError(message="Role already exists for tenant.") from e
|
||||
|
||||
await session.commit()
|
||||
await session.refresh(role)
|
||||
|
|
|
|||
|
|
@ -35,5 +35,5 @@ async def create_tenant(tenant_name: str, user_id: UUID) -> UUID:
|
|||
await session.merge(user)
|
||||
await session.commit()
|
||||
return tenant.id
|
||||
except IntegrityError:
|
||||
raise EntityAlreadyExistsError(message="Tenant already exists.")
|
||||
except IntegrityError as e:
|
||||
raise EntityAlreadyExistsError(message="Tenant already exists.") from e
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue