Skip to content

Commit 396d9ac

Browse files
committed
refac
1 parent 67a7b23 commit 396d9ac

8 files changed

Lines changed: 75 additions & 35 deletions

File tree

backend/open_webui/routers/audio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from open_webui.models.config import Config
5959
from open_webui.utils.access_control import has_permission
6060
from open_webui.utils.auth import get_admin_user, get_verified_user
61+
from open_webui.utils.errors import error_detail
6162
from open_webui.utils.headers import include_user_info_headers
6263
from open_webui.utils.misc import strict_match_mime_type
6364
from open_webui.utils.session_pool import get_session
@@ -1055,7 +1056,7 @@ async def transcribe(request: Request, file_path: str, metadata: Optional[dict]
10551056
log.exception(e)
10561057
raise HTTPException(
10571058
status_code=status.HTTP_400_BAD_REQUEST,
1058-
detail=ERROR_MESSAGES.DEFAULT(e),
1059+
detail=error_detail(e, 'Error processing audio file'),
10591060
)
10601061

10611062
results = []

backend/open_webui/routers/functions.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FunctionWithValvesModel,
2323
)
2424
from open_webui.utils.auth import get_admin_user, get_verified_user
25+
from open_webui.utils.errors import error_detail
2526
from open_webui.utils.plugin import (
2627
get_functions_cache,
2728
get_function_module_from_cache,
@@ -133,7 +134,7 @@ async def load_function_from_url(request: Request, form_data: LoadUrlForm, user=
133134
'content': data,
134135
}
135136
except Exception as e:
136-
raise HTTPException(status_code=500, detail=ERROR_MESSAGES.DEFAULT(e))
137+
raise HTTPException(status_code=500, detail=error_detail(e, 'Error fetching function'))
137138

138139

139140
############################
@@ -173,7 +174,7 @@ async def sync_functions(
173174
log.exception(f'Failed to load a function: {e}')
174175
raise HTTPException(
175176
status_code=status.HTTP_400_BAD_REQUEST,
176-
detail=ERROR_MESSAGES.DEFAULT(e),
177+
detail=error_detail(e, 'Error loading function'),
177178
)
178179

179180

@@ -236,7 +237,7 @@ async def create_new_function(
236237
log.exception(f'Failed to create a new function: {e}')
237238
raise HTTPException(
238239
status_code=status.HTTP_400_BAD_REQUEST,
239-
detail=ERROR_MESSAGES.DEFAULT(e),
240+
detail=error_detail(e, 'Error creating function'),
240241
)
241242
else:
242243
raise HTTPException(
@@ -384,7 +385,7 @@ async def update_function_by_id(
384385
except Exception as e:
385386
raise HTTPException(
386387
status_code=status.HTTP_400_BAD_REQUEST,
387-
detail=ERROR_MESSAGES.DEFAULT(e),
388+
detail=error_detail(e, 'Error updating function'),
388389
)
389390

390391

@@ -432,7 +433,7 @@ async def get_function_valves_by_id(
432433
except Exception as e:
433434
raise HTTPException(
434435
status_code=status.HTTP_400_BAD_REQUEST,
435-
detail=ERROR_MESSAGES.DEFAULT(e),
436+
detail=error_detail(e, 'Error getting function valves'),
436437
)
437438
else:
438439
raise HTTPException(
@@ -508,7 +509,7 @@ async def update_function_valves_by_id(
508509
log.exception(f'Error updating function values by id {id}: {e}')
509510
raise HTTPException(
510511
status_code=status.HTTP_400_BAD_REQUEST,
511-
detail=ERROR_MESSAGES.DEFAULT(e),
512+
detail=error_detail(e, 'Error updating function valves'),
512513
)
513514
else:
514515
raise HTTPException(
@@ -540,7 +541,7 @@ async def get_function_user_valves_by_id(
540541
except Exception as e:
541542
raise HTTPException(
542543
status_code=status.HTTP_400_BAD_REQUEST,
543-
detail=ERROR_MESSAGES.DEFAULT(e),
544+
detail=error_detail(e, 'Error getting function user valves'),
544545
)
545546
else:
546547
raise HTTPException(
@@ -607,7 +608,7 @@ async def update_function_user_valves_by_id(
607608
log.exception(f'Error updating function user valves by id {id}: {e}')
608609
raise HTTPException(
609610
status_code=status.HTTP_400_BAD_REQUEST,
610-
detail=ERROR_MESSAGES.DEFAULT(e),
611+
detail=error_detail(e, 'Error updating function user valves'),
611612
)
612613
else:
613614
raise HTTPException(

backend/open_webui/routers/groups.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from open_webui.models.tools import Tools
2323
from open_webui.models.users import UserInfoResponse, Users
2424
from open_webui.utils.auth import get_admin_user, get_verified_user
25+
from open_webui.utils.errors import error_detail
2526
from sqlalchemy.ext.asyncio import AsyncSession
2627

2728
log = logging.getLogger(__name__)
@@ -87,7 +88,7 @@ async def create_new_group(
8788
log.exception(f'Error creating a new group: {e}')
8889
raise HTTPException(
8990
status_code=status.HTTP_400_BAD_REQUEST,
90-
detail=ERROR_MESSAGES.DEFAULT(e),
91+
detail=error_detail(e, 'Error creating group'),
9192
)
9293

9394

@@ -166,7 +167,7 @@ async def get_users_in_group(id: str, user=Depends(get_admin_user), db: AsyncSes
166167
log.exception(f'Error adding users to group {id}: {e}')
167168
raise HTTPException(
168169
status_code=status.HTTP_400_BAD_REQUEST,
169-
detail=ERROR_MESSAGES.DEFAULT(e),
170+
detail=error_detail(e, 'Error getting group members'),
170171
)
171172

172173

@@ -206,7 +207,7 @@ async def update_group_by_id(
206207
log.exception(f'Error updating group {id}: {e}')
207208
raise HTTPException(
208209
status_code=status.HTTP_400_BAD_REQUEST,
209-
detail=ERROR_MESSAGES.DEFAULT(e),
210+
detail=error_detail(e, 'Error updating group'),
210211
)
211212

212213

@@ -249,7 +250,7 @@ async def add_user_to_group(
249250
log.exception(f'Error adding users to group {id}: {e}')
250251
raise HTTPException(
251252
status_code=status.HTTP_400_BAD_REQUEST,
252-
detail=ERROR_MESSAGES.DEFAULT(e),
253+
detail=error_detail(e, 'Error adding users to group'),
253254
)
254255

255256

@@ -284,7 +285,7 @@ async def remove_users_from_group(
284285
log.exception(f'Error removing users from group {id}: {e}')
285286
raise HTTPException(
286287
status_code=status.HTTP_400_BAD_REQUEST,
287-
detail=ERROR_MESSAGES.DEFAULT(e),
288+
detail=error_detail(e, 'Error removing users from group'),
288289
)
289290

290291

@@ -316,7 +317,7 @@ async def delete_group_by_id(
316317
log.exception(f'Error deleting group {id}: {e}')
317318
raise HTTPException(
318319
status_code=status.HTTP_400_BAD_REQUEST,
319-
detail=ERROR_MESSAGES.DEFAULT(e),
320+
detail=error_detail(e, 'Error deleting group'),
320321
)
321322

322323

backend/open_webui/routers/images.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from open_webui.routers.files import get_file_content_by_id, upload_file_handler
3232
from open_webui.utils.access_control import has_permission
3333
from open_webui.utils.auth import get_admin_user, get_verified_user
34+
from open_webui.utils.errors import error_detail
3435
from open_webui.utils.headers import include_user_info_headers
3536
from open_webui.utils.images.comfyui import (
3637
ComfyUICreateImageForm,
@@ -166,7 +167,11 @@ async def get_image_model(request):
166167
options = await r.json()
167168
return options['sd_model_checkpoint']
168169
except Exception as e:
169-
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
170+
log.exception(f'Failed to get default model from automatic1111: {e}')
171+
raise HTTPException(
172+
status_code=400,
173+
detail=error_detail(e, 'Failed to connect to the image generation engine'),
174+
)
170175

171176

172177
class ImagesConfig(BaseModel):
@@ -383,7 +388,8 @@ async def get_models(request: Request, user=Depends(get_verified_user)):
383388
)
384389
)
385390
except Exception as e:
386-
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
391+
log.exception(f'Failed to list image generation models: {e}')
392+
raise HTTPException(status_code=400, detail=error_detail(e, 'Failed to retrieve image generation models'))
387393

388394

389395
class CreateImageForm(BaseModel):
@@ -900,7 +906,7 @@ async def load_url_image(data):
900906
# Load all images in parallel for better performance
901907
form_data.image = list(await asyncio.gather(*[load_url_image(img) for img in form_data.image]))
902908
except Exception as e:
903-
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
909+
raise HTTPException(status_code=400, detail=error_detail(e, 'Error loading image'))
904910

905911
def get_image_file_item(base64_string, param_name='image'):
906912
data = base64_string

backend/open_webui/routers/retrieval.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
from open_webui.utils.access_control import has_permission
119119
from open_webui.utils.access_control.files import has_access_to_file
120120
from open_webui.utils.auth import get_admin_user, get_verified_user
121+
from open_webui.utils.errors import error_detail
121122
from open_webui.utils.misc import (
122123
calculate_sha256_string,
123124
sanitize_text_for_db,
@@ -182,7 +183,7 @@ def get_rf(
182183

183184
except Exception as e:
184185
log.error(f'ColBERT: {e}')
185-
raise Exception(ERROR_MESSAGES.DEFAULT(e))
186+
raise Exception(error_detail(e, 'Error loading reranking model'))
186187
else:
187188
if engine == 'external':
188189
try:
@@ -196,7 +197,7 @@ def get_rf(
196197
)
197198
except Exception as e:
198199
log.error(f'ExternalReranking: {e}')
199-
raise Exception(ERROR_MESSAGES.DEFAULT(e))
200+
raise Exception(error_detail(e, 'Error loading reranking model'))
200201
else:
201202
import sentence_transformers
202203
import torch
@@ -605,7 +606,7 @@ async def update_embedding_config(request: Request, form_data: EmbeddingModelUpd
605606
log.exception(f'Problem updating embedding model: {e}')
606607
raise HTTPException(
607608
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
608-
detail=ERROR_MESSAGES.DEFAULT(e),
609+
detail=error_detail(e, 'Error updating embedding configuration'),
609610
)
610611

611612

@@ -1207,7 +1208,7 @@ async def update_rag_config(request: Request, form_data: ConfigForm, user=Depend
12071208
log.exception(f'Problem updating reranking model: {e}')
12081209
raise HTTPException(
12091210
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
1210-
detail=ERROR_MESSAGES.DEFAULT(e),
1211+
detail=error_detail(e, 'Error updating reranking configuration'),
12111212
)
12121213

12131214
# Chunking settings
@@ -2149,7 +2150,7 @@ async def process_web(
21492150
log.exception(e)
21502151
raise HTTPException(
21512152
status_code=status.HTTP_400_BAD_REQUEST,
2152-
detail=ERROR_MESSAGES.DEFAULT(e),
2153+
detail=error_detail(e, 'Error querying knowledge base'),
21532154
)
21542155

21552156

@@ -2654,7 +2655,10 @@ async def search_query_with_semaphore(query):
26542655
}
26552656
except Exception as e:
26562657
log.exception('Web search content loading failed')
2657-
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT(e))
2658+
raise HTTPException(
2659+
status.HTTP_400_BAD_REQUEST,
2660+
detail=error_detail(e, ERROR_MESSAGES.WEB_SEARCH_ERROR()),
2661+
)
26582662

26592663

26602664
async def _validate_collection_access(collection_names: list[str], user, access_type: str = 'read') -> None:
@@ -2732,7 +2736,7 @@ async def query_doc_handler(
27322736
log.exception(e)
27332737
raise HTTPException(
27342738
status_code=status.HTTP_400_BAD_REQUEST,
2735-
detail=ERROR_MESSAGES.DEFAULT(e),
2739+
detail=error_detail(e, 'Error querying knowledge base'),
27362740
)
27372741

27382742

@@ -2798,7 +2802,7 @@ async def query_collection_handler(
27982802
log.exception(e)
27992803
raise HTTPException(
28002804
status_code=status.HTTP_400_BAD_REQUEST,
2801-
detail=ERROR_MESSAGES.DEFAULT(e),
2805+
detail=error_detail(e, 'Error querying knowledge base'),
28022806
)
28032807

28042808

backend/open_webui/routers/skills.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from open_webui.utils.access_control import filter_allowed_access_grants, has_permission
2222
from open_webui.utils.auth import get_admin_user, get_verified_user
23+
from open_webui.utils.errors import error_detail
2324
from pydantic import BaseModel
2425
from sqlalchemy.ext.asyncio import AsyncSession
2526

@@ -213,7 +214,7 @@ async def create_new_skill(
213214
log.exception(f'Failed to create skill: {e}')
214215
raise HTTPException(
215216
status_code=status.HTTP_400_BAD_REQUEST,
216-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
217+
detail=error_detail(e, 'Error creating skill'),
217218
)
218219

219220

@@ -337,7 +338,7 @@ async def update_skill_by_id(
337338
except Exception as e:
338339
raise HTTPException(
339340
status_code=status.HTTP_400_BAD_REQUEST,
340-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
341+
detail=error_detail(e, 'Error updating skill'),
341342
)
342343

343344

backend/open_webui/routers/tools.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
has_permission,
3232
)
3333
from open_webui.utils.auth import get_admin_user, get_verified_user
34+
from open_webui.utils.errors import error_detail
3435
from open_webui.utils.plugin import (
3536
get_tools_cache,
3637
get_tool_module_from_cache,
@@ -295,7 +296,7 @@ async def load_tool_from_url(request: Request, form_data: LoadUrlForm, user=Depe
295296
'content': data,
296297
}
297298
except Exception as e:
298-
raise HTTPException(status_code=500, detail=ERROR_MESSAGES.DEFAULT(e))
299+
raise HTTPException(status_code=500, detail=error_detail(e, 'Error fetching tool'))
299300

300301

301302
############################
@@ -404,7 +405,7 @@ async def create_new_tools(
404405
log.exception(f'Failed to load the tool by id {form_data.id}: {e}')
405406
raise HTTPException(
406407
status_code=status.HTTP_400_BAD_REQUEST,
407-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
408+
detail=error_detail(e, 'Error creating tool'),
408409
)
409410
else:
410411
raise HTTPException(
@@ -554,7 +555,7 @@ async def update_tools_by_id(
554555
except Exception as e:
555556
raise HTTPException(
556557
status_code=status.HTTP_400_BAD_REQUEST,
557-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
558+
detail=error_detail(e, 'Error updating tool'),
558559
)
559560

560561

@@ -707,7 +708,7 @@ async def get_tools_valves_by_id(
707708
except Exception as e:
708709
raise HTTPException(
709710
status_code=status.HTTP_400_BAD_REQUEST,
710-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
711+
detail=error_detail(e, 'Error getting tool valves'),
711712
)
712713

713714

@@ -818,7 +819,7 @@ async def update_tools_valves_by_id(
818819
log.exception(f'Failed to update tool valves by id {id}: {e}')
819820
raise HTTPException(
820821
status_code=status.HTTP_400_BAD_REQUEST,
821-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
822+
detail=error_detail(e, 'Error updating tool valves'),
822823
)
823824

824825

@@ -860,7 +861,7 @@ async def get_tools_user_valves_by_id(
860861
except Exception as e:
861862
raise HTTPException(
862863
status_code=status.HTTP_400_BAD_REQUEST,
863-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
864+
detail=error_detail(e, 'Error getting tool user valves'),
864865
)
865866

866867

@@ -958,7 +959,7 @@ async def update_tools_user_valves_by_id(
958959
log.exception(f'Failed to update user valves by id {id}: {e}')
959960
raise HTTPException(
960961
status_code=status.HTTP_400_BAD_REQUEST,
961-
detail=ERROR_MESSAGES.DEFAULT(str(e)),
962+
detail=error_detail(e, 'Error updating tool user valves'),
962963
)
963964
else:
964965
raise HTTPException(

0 commit comments

Comments
 (0)