Qurrah commited on
Commit
d6abbac
·
1 Parent(s): 6bcbdd0

REFINE: enhance error handling for workspace existence and update cache key format in query service

Browse files
api/routes/documents.py CHANGED
@@ -24,8 +24,8 @@ async def ingest_document(
24
  background_tasks: BackgroundTasks,
25
  _: str = Security(verify_api_key),
26
  ):
27
- validate_supported_mime(req.mime)
28
- ensure_workspace_exists(req.workspace_slug)
29
 
30
  queue_document_ingest(
31
  background_tasks,
 
24
  background_tasks: BackgroundTasks,
25
  _: str = Security(verify_api_key),
26
  ):
27
+ validate_supported_mime(req.mime) # → 400 via ValueError handler
28
+ ensure_workspace_exists(req.workspace_slug) # → 404 via WorkspaceNotFoundError handler
29
 
30
  queue_document_ingest(
31
  background_tasks,
services/document_service.py CHANGED
@@ -1,5 +1,6 @@
1
  import logging
2
 
 
3
  from repositories.document import (
4
  delete_document_status,
5
  get_document_status as get_document_status_record,
@@ -45,7 +46,11 @@ def update_status(
45
  error=error,
46
  chunks=chunks,
47
  )
48
-
 
 
 
 
49
 
50
  def queue_document_ingest(background_tasks, **kwargs) -> None:
51
  background_tasks.add_task(process_document, **kwargs)
 
1
  import logging
2
 
3
+ from core.exceptions import WorkspaceNotFoundError
4
  from repositories.document import (
5
  delete_document_status,
6
  get_document_status as get_document_status_record,
 
46
  error=error,
47
  chunks=chunks,
48
  )
49
+ def ensure_workspace_exists(workspace_slug: str) -> str:
50
+ workspace_id = get_workspace_id_by_slug(workspace_slug)
51
+ if not workspace_id:
52
+ raise WorkspaceNotFoundError(f"Workspace '{workspace_slug}' not found")
53
+ return workspace_id
54
 
55
  def queue_document_ingest(background_tasks, **kwargs) -> None:
56
  background_tasks.add_task(process_document, **kwargs)
services/query_service.py CHANGED
@@ -143,7 +143,8 @@ def answer_query_request(
143
  if post_caption:
144
  enriched_query = f"Post context: {post_caption}\nUser question: {query}"
145
 
146
- cache_key = f"query:{channel_identifier}:{query.strip().lower()}"
 
147
  cached_reply = inventory_cache.get(cache_key)
148
  if cached_reply:
149
  logger.info(f"Cache hit for query='{query[:60]}'")
 
143
  if post_caption:
144
  enriched_query = f"Post context: {post_caption}\nUser question: {query}"
145
 
146
+ caption_part = post_caption.strip().lower() if post_caption else ""
147
+ cache_key = f"query:{channel_identifier}:{query.strip().lower()}:{caption_part}"
148
  cached_reply = inventory_cache.get(cache_key)
149
  if cached_reply:
150
  logger.info(f"Cache hit for query='{query[:60]}'")