From 83f086ccdddc31ae10c7c298bd8952da9959c629 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Mon, 6 May 2024 16:54:15 +0800 Subject: [PATCH] fix: do not return raw search exception due to API keys in URLs --- backend/apps/rag/main.py | 9 ++++++++- backend/constants.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 3923eb459..97cf9977b 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -556,7 +556,14 @@ def resolve_hostname(hostname): @app.post("/websearch") def store_websearch(form_data: SearchForm, user=Depends(get_current_user)): try: - web_results = search_web(form_data.query) + try: + web_results = search_web(form_data.query) + except Exception as e: + log.exception(e) + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.WEB_SEARCH_ERROR, + ) urls = [result.link for result in web_results] loader = get_web_loader(urls) data = loader.load() diff --git a/backend/constants.py b/backend/constants.py index 3fdf506fa..d30f2b9f6 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -75,3 +75,7 @@ class ERROR_MESSAGES(str, Enum): INVALID_URL = ( "Oops! The URL you provided is invalid. Please double-check and try again." ) + + WEB_SEARCH_ERROR = ( + "Oops! Something went wrong while searching the web. Please try again later." + )