added fix that raises error if database doesnt exist
This commit is contained in:
parent
310e9e97ae
commit
ce685557bb
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