From d5e69f182cd7a6371ab25248f6432b277f83ef23 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 20 Apr 2026 08:53:06 +0900 Subject: [PATCH] refac --- backend/open_webui/main.py | 7 ++--- backend/open_webui/routers/pipelines.py | 35 ++++++++++++++++++++++--- backend/open_webui/utils/chat.py | 4 ++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index d1a0bddce..ba7f74c83 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1877,7 +1877,8 @@ async def chat_completion( finally: raise # re-raise to ensure proper task cancellation handling except Exception as e: - log.error('Error processing chat payload: %s', e) + error_detail = e.detail if isinstance(e, HTTPException) else str(e) + log.error('Error processing chat payload: %s', error_detail) if metadata.get('chat_id') and metadata.get('message_id'): # Update the chat message with the error try: @@ -1887,7 +1888,7 @@ async def chat_completion( metadata['message_id'], { 'parentId': metadata.get('user_message_id', None), - 'error': {'content': str(e)}, + 'error': {'content': error_detail}, }, ) @@ -1896,7 +1897,7 @@ async def chat_completion( await event_emitter( { 'type': 'chat:message:error', - 'data': {'error': {'content': str(e)}}, + 'data': {'error': {'content': error_detail}}, } ) await event_emitter( diff --git a/backend/open_webui/routers/pipelines.py b/backend/open_webui/routers/pipelines.py index 94c1357fd..580fb42fb 100644 --- a/backend/open_webui/routers/pipelines.py +++ b/backend/open_webui/routers/pipelines.py @@ -94,9 +94,24 @@ async def process_pipeline_inlet_filter(request, payload, user, models): response.raise_for_status() payload = await response.json() except aiohttp.ClientResponseError as e: - res = await response.json() if response.content_type == 'application/json' else {} - if 'detail' in res: - raise Exception(response.status, res['detail']) + try: + res = await response.json() if 'application/json' in response.content_type else {} + if 'detail' in res: + raise HTTPException( + status_code=response.status, + detail=res['detail'], + ) + except HTTPException: + raise + except Exception: + pass + + raise HTTPException( + status_code=response.status, + detail=e.message, + ) + except HTTPException: + raise except Exception as e: log.exception(f'Connection error: {e}') @@ -146,9 +161,21 @@ async def process_pipeline_outlet_filter(request, payload, user, models): try: res = await response.json() if 'application/json' in response.content_type else {} if 'detail' in res: - raise Exception(response.status, res) + raise HTTPException( + status_code=response.status, + detail=res['detail'], + ) + except HTTPException: + raise except Exception: pass + + raise HTTPException( + status_code=response.status, + detail=e.message, + ) + except HTTPException: + raise except Exception as e: log.exception(f'Connection error: {e}') diff --git a/backend/open_webui/utils/chat.py b/backend/open_webui/utils/chat.py index 3539d57c8..9899469da 100644 --- a/backend/open_webui/utils/chat.py +++ b/backend/open_webui/utils/chat.py @@ -10,7 +10,7 @@ import json import uuid import asyncio -from fastapi import Request, status +from fastapi import HTTPException, Request, status from starlette.responses import Response, StreamingResponse, JSONResponse @@ -328,6 +328,8 @@ async def chat_completed(request: Request, form_data: dict, user: Any): try: data = await process_pipeline_outlet_filter(request, data, user, models) + except HTTPException: + raise except Exception as e: raise Exception(f'Error: {e}')