From 8f6369374d074aa873843ffe9c745151d87df0c6 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 4 Sep 2024 17:52:59 +0200 Subject: [PATCH] refac: error handling --- backend/open_webui/main.py | 70 +++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 5ff540861..12d144342 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -739,10 +739,16 @@ class PipelineMiddleware(BaseHTTPMiddleware): try: data = filter_pipeline(data, user) except Exception as e: - return JSONResponse( - status_code=e.args[0], - content={"detail": e.args[1]}, - ) + if len(e.args) > 1: + return JSONResponse( + status_code=e.args[0], + content={"detail": e.args[1]}, + ) + else: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content={"detail": str(e)}, + ) modified_body_bytes = json.dumps(data).encode("utf-8") # Replace the request body with the modified one @@ -1390,10 +1396,16 @@ async def generate_title(form_data: dict, user=Depends(get_verified_user)): try: payload = filter_pipeline(payload, user) except Exception as e: - return JSONResponse( - status_code=e.args[0], - content={"detail": e.args[1]}, - ) + if len(e.args) > 1: + return JSONResponse( + status_code=e.args[0], + content={"detail": e.args[1]}, + ) + else: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content={"detail": str(e)}, + ) if "chat_id" in payload: del payload["chat_id"] @@ -1443,10 +1455,16 @@ async def generate_search_query(form_data: dict, user=Depends(get_verified_user) try: payload = filter_pipeline(payload, user) except Exception as e: - return JSONResponse( - status_code=e.args[0], - content={"detail": e.args[1]}, - ) + if len(e.args) > 1: + return JSONResponse( + status_code=e.args[0], + content={"detail": e.args[1]}, + ) + else: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content={"detail": str(e)}, + ) if "chat_id" in payload: del payload["chat_id"] @@ -1500,10 +1518,16 @@ Message: """{{prompt}}""" try: payload = filter_pipeline(payload, user) except Exception as e: - return JSONResponse( - status_code=e.args[0], - content={"detail": e.args[1]}, - ) + if len(e.args) > 1: + return JSONResponse( + status_code=e.args[0], + content={"detail": e.args[1]}, + ) + else: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content={"detail": str(e)}, + ) if "chat_id" in payload: del payload["chat_id"] @@ -1552,10 +1576,16 @@ Responses from models: {{responses}}""" try: payload = filter_pipeline(payload, user) except Exception as e: - return JSONResponse( - status_code=e.args[0], - content={"detail": e.args[1]}, - ) + if len(e.args) > 1: + return JSONResponse( + status_code=e.args[0], + content={"detail": e.args[1]}, + ) + else: + return JSONResponse( + status_code=status.HTTP_400_BAD_REQUEST, + content={"detail": str(e)}, + ) if "chat_id" in payload: del payload["chat_id"]