From 86a1e4360599060be31879da3b3d24961367c81b Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 11 Jan 2023 03:01:09 -0300 Subject: [PATCH] Return HTTP Exception on /api/update API call failure - Previously the backend was just throwing backend error. The frontend calling the /update API wasn't getting notified - Now the frontend can react appropriately and make the issue visible to the user --- src/routers/api.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/routers/api.py b/src/routers/api.py index 0da30eb3..9480c914 100644 --- a/src/routers/api.py +++ b/src/routers/api.py @@ -1,11 +1,11 @@ # Standard Packages import yaml -import time import logging from typing import Optional # External Packages from fastapi import APIRouter +from fastapi import HTTPException # Internal Packages from src.configure import configure_processor, configure_search @@ -114,12 +114,22 @@ def search(q: str, n: Optional[int] = 5, t: Optional[SearchType] = None, r: Opti @api.get('/update') def update(t: Optional[SearchType] = None, force: Optional[bool] = False): - state.search_index_lock.acquire() - state.model = configure_search(state.model, state.config, regenerate=force, t=t) - state.search_index_lock.release() - logger.info("Search Index updated via API call") + try: + state.search_index_lock.acquire() + state.model = configure_search(state.model, state.config, regenerate=force, t=t) + state.search_index_lock.release() + except ValueError as e: + logger.error(e) + raise HTTPException(status_code=500, detail=str(e)) + else: + logger.info("Search Index updated via API call") - state.processor_config = configure_processor(state.config.processor) - logger.info("Processor reconfigured via API call") + try: + state.processor_config = configure_processor(state.config.processor) + except ValueError as e: + logger.error(e) + raise HTTPException(status_code=500, detail=str(e)) + else: + logger.info("Processor reconfigured via API call") return {'status': 'ok', 'message': 'khoj reloaded'}