refac: error handling

This commit is contained in:
Timothy Jaeryang Baek 2025-03-28 00:52:13 -07:00
parent 2fc8ace46d
commit 5656f030c4
4 changed files with 32 additions and 9 deletions

View File

@ -89,6 +89,7 @@ from open_webui.internal.db import Session, engine
from open_webui.models.functions import Functions from open_webui.models.functions import Functions
from open_webui.models.models import Models from open_webui.models.models import Models
from open_webui.models.users import UserModel, Users from open_webui.models.users import UserModel, Users
from open_webui.models.chats import Chats
from open_webui.config import ( from open_webui.config import (
LICENSE_KEY, LICENSE_KEY,
@ -428,7 +429,10 @@ app = FastAPI(
oauth_manager = OAuthManager(app) oauth_manager = OAuthManager(app)
app.state.config = AppConfig(redis_url=REDIS_URL, redis_sentinels=get_sentinels_from_env(REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT)) app.state.config = AppConfig(
redis_url=REDIS_URL,
redis_sentinels=get_sentinels_from_env(REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT),
)
app.state.WEBUI_NAME = WEBUI_NAME app.state.WEBUI_NAME = WEBUI_NAME
app.state.LICENSE_METADATA = None app.state.LICENSE_METADATA = None
@ -1084,6 +1088,14 @@ async def chat_completion(
except Exception as e: except Exception as e:
log.debug(f"Error processing chat payload: {e}") log.debug(f"Error processing chat payload: {e}")
Chats.upsert_message_to_chat_by_id_and_message_id(
metadata["chat_id"],
metadata["message_id"],
{
"error": {"content": str(e)},
},
)
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e), detail=str(e),

View File

@ -101,7 +101,7 @@ async def process_filter_functions(
form_data = handler(**params) form_data = handler(**params)
except Exception as e: except Exception as e:
log.exception(f"Error in {filter_type} handler {filter_id}: {e}") log.debug(f"Error in {filter_type} handler {filter_id}: {e}")
raise e raise e
# Handle file cleanup for inlet # Handle file cleanup for inlet

View File

@ -18,9 +18,7 @@ from uuid import uuid4
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from fastapi import Request from fastapi import Request, HTTPException
from fastapi import BackgroundTasks
from starlette.responses import Response, StreamingResponse from starlette.responses import Response, StreamingResponse
@ -1046,6 +1044,16 @@ async def process_chat_response(
# Non-streaming response # Non-streaming response
if not isinstance(response, StreamingResponse): if not isinstance(response, StreamingResponse):
if event_emitter: if event_emitter:
if "error" in response:
error = response["error"].get("detail", response["error"])
Chats.upsert_message_to_chat_by_id_and_message_id(
metadata["chat_id"],
metadata["message_id"],
{
"error": {"content": error},
},
)
if "selected_model_id" in response: if "selected_model_id" in response:
Chats.upsert_message_to_chat_by_id_and_message_id( Chats.upsert_message_to_chat_by_id_and_message_id(
metadata["chat_id"], metadata["chat_id"],
@ -1055,7 +1063,8 @@ async def process_chat_response(
}, },
) )
if response.get("choices", [])[0].get("message", {}).get("content"): choices = response.get("choices", [])
if choices and choices[0].get("message", {}).get("content"):
content = response["choices"][0]["message"]["content"] content = response["choices"][0]["message"]["content"]
if content: if content:

View File

@ -1639,10 +1639,12 @@
return null; return null;
}); });
console.log(res);
if (res) { if (res) {
taskId = res.task_id; if (res.error) {
await handleOpenAIError(res.error, responseMessage);
} else {
taskId = res.task_id;
}
} }
await tick(); await tick();