fix: Better error message if cognee is run without cognee.add and cognee.cognify (#1940)
<!-- .github/pull_request_template.md -->
## Description
<!--
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->
## Acceptance Criteria
<!--
* Key requirements to the new feature or modification;
* Proof that the changes work and meet the requirements;
* Include instructions on how to verify the changes. Describe how to
test it locally;
* Proof that it's sufficiently tested.
-->
## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):
## Screenshots/Videos (if applicable)
<!-- Add screenshots or videos to help explain your changes -->
## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [ ] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages
## 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 is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved error messaging in search functionality with clearer,
actionable feedback when database or user configuration prerequisites
are not met
* Standardized error response format for consistent and informative
error reporting across search operations
<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
commit
2c23063da7
2 changed files with 29 additions and 2 deletions
|
|
@ -8,12 +8,14 @@ from fastapi.encoders import jsonable_encoder
|
|||
|
||||
from cognee.modules.search.types import SearchType, SearchResult, CombinedSearchResult
|
||||
from cognee.api.DTO import InDTO, OutDTO
|
||||
from cognee.modules.users.exceptions.exceptions import PermissionDeniedError
|
||||
from cognee.modules.users.exceptions.exceptions import PermissionDeniedError, UserNotFoundError
|
||||
from cognee.modules.users.models import User
|
||||
from cognee.modules.search.operations import get_history
|
||||
from cognee.modules.users.methods import get_authenticated_user
|
||||
from cognee.shared.utils import send_telemetry
|
||||
from cognee import __version__ as cognee_version
|
||||
from cognee.infrastructure.databases.exceptions import DatabaseNotCreatedError
|
||||
from cognee.exceptions import CogneeValidationError
|
||||
|
||||
|
||||
# Note: Datasets sent by name will only map to datasets owned by the request sender
|
||||
|
|
@ -138,6 +140,17 @@ def get_search_router() -> APIRouter:
|
|||
)
|
||||
|
||||
return jsonable_encoder(results)
|
||||
except (DatabaseNotCreatedError, UserNotFoundError, CogneeValidationError) as e:
|
||||
# Return a clear 422 with actionable guidance instead of leaking a stacktrace
|
||||
status_code = getattr(e, "status_code", 422)
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content={
|
||||
"error": "Search prerequisites not met",
|
||||
"detail": str(e),
|
||||
"hint": "Run `await cognee.add(...)` then `await cognee.cognify()` before searching.",
|
||||
},
|
||||
)
|
||||
except PermissionDeniedError:
|
||||
return []
|
||||
except Exception as error:
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ from cognee.modules.data.methods import get_authorized_existing_datasets
|
|||
from cognee.modules.data.exceptions import DatasetNotFoundError
|
||||
from cognee.context_global_variables import set_session_user_context_variable
|
||||
from cognee.shared.logging_utils import get_logger
|
||||
from cognee.infrastructure.databases.exceptions import DatabaseNotCreatedError
|
||||
from cognee.exceptions import CogneeValidationError
|
||||
from cognee.modules.users.exceptions.exceptions import UserNotFoundError
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
|
@ -176,7 +179,18 @@ async def search(
|
|||
datasets = [datasets]
|
||||
|
||||
if user is None:
|
||||
user = await get_default_user()
|
||||
try:
|
||||
user = await get_default_user()
|
||||
except (DatabaseNotCreatedError, UserNotFoundError) as error:
|
||||
# Provide a clear, actionable message instead of surfacing low-level stacktraces
|
||||
raise CogneeValidationError(
|
||||
message=(
|
||||
"Search prerequisites not met: no database/default user found. "
|
||||
"Initialize Cognee before searching by:\n"
|
||||
"• running `await cognee.add(...)` followed by `await cognee.cognify()`."
|
||||
),
|
||||
name="SearchPreconditionError",
|
||||
) from error
|
||||
|
||||
await set_session_user_context_variable(user)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue