Skip to content

Commit 0883638

Browse files
committed
refac
1 parent ee5de69 commit 0883638

7 files changed

Lines changed: 86 additions & 43 deletions

File tree

backend/open_webui/routers/audio.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
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
6261
from open_webui.utils.headers import include_user_info_headers
6362
from open_webui.utils.misc import strict_match_mime_type
6463
from open_webui.utils.session_pool import get_session
@@ -1056,7 +1055,7 @@ async def transcribe(request: Request, file_path: str, metadata: Optional[dict]
10561055
log.exception(e)
10571056
raise HTTPException(
10581057
status_code=status.HTTP_400_BAD_REQUEST,
1059-
detail=error_detail(e, 'Error processing audio file'),
1058+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error processing audio file'),
10601059
)
10611060

10621061
results = []

backend/open_webui/routers/functions.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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
2625
from open_webui.utils.plugin import (
2726
get_functions_cache,
2827
get_function_module_from_cache,
@@ -133,8 +132,13 @@ async def load_function_from_url(request: Request, form_data: LoadUrlForm, user=
133132
'name': function_name,
134133
'content': data,
135134
}
135+
except HTTPException:
136+
raise
136137
except Exception as e:
137-
raise HTTPException(status_code=500, detail=error_detail(e, 'Error fetching function'))
138+
raise HTTPException(
139+
status_code=500,
140+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error fetching function'),
141+
)
138142

139143

140144
############################
@@ -174,7 +178,7 @@ async def sync_functions(
174178
log.exception(f'Failed to load a function: {e}')
175179
raise HTTPException(
176180
status_code=status.HTTP_400_BAD_REQUEST,
177-
detail=error_detail(e, 'Error loading function'),
181+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error loading function'),
178182
)
179183

180184

@@ -233,11 +237,13 @@ async def create_new_function(
233237
status_code=status.HTTP_400_BAD_REQUEST,
234238
detail=ERROR_MESSAGES.DEFAULT('Error creating function'),
235239
)
240+
except HTTPException:
241+
raise
236242
except Exception as e:
237243
log.exception(f'Failed to create a new function: {e}')
238244
raise HTTPException(
239245
status_code=status.HTTP_400_BAD_REQUEST,
240-
detail=error_detail(e, 'Error creating function'),
246+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error creating function'),
241247
)
242248
else:
243249
raise HTTPException(
@@ -382,10 +388,12 @@ async def update_function_by_id(
382388
detail=ERROR_MESSAGES.DEFAULT('Error updating function'),
383389
)
384390

391+
except HTTPException:
392+
raise
385393
except Exception as e:
386394
raise HTTPException(
387395
status_code=status.HTTP_400_BAD_REQUEST,
388-
detail=error_detail(e, 'Error updating function'),
396+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error updating function'),
389397
)
390398

391399

@@ -433,7 +441,7 @@ async def get_function_valves_by_id(
433441
except Exception as e:
434442
raise HTTPException(
435443
status_code=status.HTTP_400_BAD_REQUEST,
436-
detail=error_detail(e, 'Error getting function valves'),
444+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error getting function valves'),
437445
)
438446
else:
439447
raise HTTPException(
@@ -509,7 +517,7 @@ async def update_function_valves_by_id(
509517
log.exception(f'Error updating function values by id {id}: {e}')
510518
raise HTTPException(
511519
status_code=status.HTTP_400_BAD_REQUEST,
512-
detail=error_detail(e, 'Error updating function valves'),
520+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error updating function valves'),
513521
)
514522
else:
515523
raise HTTPException(
@@ -541,7 +549,7 @@ async def get_function_user_valves_by_id(
541549
except Exception as e:
542550
raise HTTPException(
543551
status_code=status.HTTP_400_BAD_REQUEST,
544-
detail=error_detail(e, 'Error getting function user valves'),
552+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error getting function user valves'),
545553
)
546554
else:
547555
raise HTTPException(
@@ -608,7 +616,7 @@ async def update_function_user_valves_by_id(
608616
log.exception(f'Error updating function user valves by id {id}: {e}')
609617
raise HTTPException(
610618
status_code=status.HTTP_400_BAD_REQUEST,
611-
detail=error_detail(e, 'Error updating function user valves'),
619+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error updating function user valves'),
612620
)
613621
else:
614622
raise HTTPException(

backend/open_webui/routers/groups.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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
2625
from sqlalchemy.ext.asyncio import AsyncSession
2726

2827
log = logging.getLogger(__name__)
@@ -84,11 +83,13 @@ async def create_new_group(
8483
status_code=status.HTTP_400_BAD_REQUEST,
8584
detail=ERROR_MESSAGES.DEFAULT('Error creating group'),
8685
)
86+
except HTTPException:
87+
raise
8788
except Exception as e:
8889
log.exception(f'Error creating a new group: {e}')
8990
raise HTTPException(
9091
status_code=status.HTTP_400_BAD_REQUEST,
91-
detail=error_detail(e, 'Error creating group'),
92+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error creating group'),
9293
)
9394

9495

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

173174

@@ -203,11 +204,13 @@ async def update_group_by_id(
203204
status_code=status.HTTP_400_BAD_REQUEST,
204205
detail=ERROR_MESSAGES.DEFAULT('Error updating group'),
205206
)
207+
except HTTPException:
208+
raise
206209
except Exception as e:
207210
log.exception(f'Error updating group {id}: {e}')
208211
raise HTTPException(
209212
status_code=status.HTTP_400_BAD_REQUEST,
210-
detail=error_detail(e, 'Error updating group'),
213+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error updating group'),
211214
)
212215

213216

@@ -246,11 +249,13 @@ async def add_user_to_group(
246249
status_code=status.HTTP_400_BAD_REQUEST,
247250
detail=ERROR_MESSAGES.DEFAULT('Error adding users to group'),
248251
)
252+
except HTTPException:
253+
raise
249254
except Exception as e:
250255
log.exception(f'Error adding users to group {id}: {e}')
251256
raise HTTPException(
252257
status_code=status.HTTP_400_BAD_REQUEST,
253-
detail=error_detail(e, 'Error adding users to group'),
258+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error adding users to group'),
254259
)
255260

256261

@@ -281,11 +286,13 @@ async def remove_users_from_group(
281286
status_code=status.HTTP_400_BAD_REQUEST,
282287
detail=ERROR_MESSAGES.DEFAULT('Error removing users from group'),
283288
)
289+
except HTTPException:
290+
raise
284291
except Exception as e:
285292
log.exception(f'Error removing users from group {id}: {e}')
286293
raise HTTPException(
287294
status_code=status.HTTP_400_BAD_REQUEST,
288-
detail=error_detail(e, 'Error removing users from group'),
295+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error removing users from group'),
289296
)
290297

291298

@@ -313,11 +320,13 @@ async def delete_group_by_id(
313320
status_code=status.HTTP_400_BAD_REQUEST,
314321
detail=ERROR_MESSAGES.DEFAULT('Error deleting group'),
315322
)
323+
except HTTPException:
324+
raise
316325
except Exception as e:
317326
log.exception(f'Error deleting group {id}: {e}')
318327
raise HTTPException(
319328
status_code=status.HTTP_400_BAD_REQUEST,
320-
detail=error_detail(e, 'Error deleting group'),
329+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error deleting group'),
321330
)
322331

323332

backend/open_webui/routers/images.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
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
3534
from open_webui.utils.headers import include_user_info_headers
3635
from open_webui.utils.images.comfyui import (
3736
ComfyUICreateImageForm,
@@ -170,7 +169,7 @@ async def get_image_model(request):
170169
log.exception(f'Failed to get default model from automatic1111: {e}')
171170
raise HTTPException(
172171
status_code=400,
173-
detail=error_detail(e, 'Failed to connect to the image generation engine'),
172+
detail=ERROR_MESSAGES.DEFAULT(e, 'Failed to connect to the image generation engine'),
174173
)
175174

176175

@@ -389,7 +388,10 @@ async def get_models(request: Request, user=Depends(get_verified_user)):
389388
)
390389
except Exception as e:
391390
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'))
391+
raise HTTPException(
392+
status_code=400,
393+
detail=ERROR_MESSAGES.DEFAULT(e, 'Failed to retrieve image generation models'),
394+
)
393395

394396

395397
class CreateImageForm(BaseModel):
@@ -905,8 +907,13 @@ async def load_url_image(data):
905907
elif isinstance(form_data.image, list):
906908
# Load all images in parallel for better performance
907909
form_data.image = list(await asyncio.gather(*[load_url_image(img) for img in form_data.image]))
910+
except HTTPException:
911+
raise
908912
except Exception as e:
909-
raise HTTPException(status_code=400, detail=error_detail(e, 'Error loading image'))
913+
raise HTTPException(
914+
status_code=400,
915+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error loading image'),
916+
)
910917

911918
def get_image_file_item(base64_string, param_name='image'):
912919
data = base64_string

backend/open_webui/routers/retrieval.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
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
122121
from open_webui.utils.misc import (
123122
calculate_sha256_string,
124123
sanitize_text_for_db,
@@ -183,7 +182,7 @@ def get_rf(
183182

184183
except Exception as e:
185184
log.error(f'ColBERT: {e}')
186-
raise Exception(error_detail(e, 'Error loading reranking model'))
185+
raise Exception(ERROR_MESSAGES.DEFAULT(e, 'Error loading reranking model'))
187186
else:
188187
if engine == 'external':
189188
try:
@@ -197,7 +196,9 @@ def get_rf(
197196
)
198197
except Exception as e:
199198
log.error(f'ExternalReranking: {e}')
200-
raise Exception(error_detail(e, 'Error loading reranking model'))
199+
raise Exception(
200+
ERROR_MESSAGES.DEFAULT(e, 'Error loading reranking model')
201+
)
201202
else:
202203
import sentence_transformers
203204
import torch
@@ -217,7 +218,7 @@ def get_rf(
217218
)
218219
except Exception as e:
219220
log.error(f'CrossEncoder: {e}')
220-
raise Exception(ERROR_MESSAGES.DEFAULT('CrossEncoder error'))
221+
raise Exception(ERROR_MESSAGES.DEFAULT(e, 'CrossEncoder error'))
221222

222223
# Safely adjust pad_token_id if missing as some models do not have this in config
223224
try:
@@ -606,7 +607,7 @@ async def update_embedding_config(request: Request, form_data: EmbeddingModelUpd
606607
log.exception(f'Problem updating embedding model: {e}')
607608
raise HTTPException(
608609
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
609-
detail=error_detail(e, 'Error updating embedding configuration'),
610+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error updating embedding configuration'),
610611
)
611612

612613

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

12141215
# Chunking settings
@@ -2146,11 +2147,13 @@ async def process_web(
21462147
'status': True,
21472148
'content': content,
21482149
}
2150+
except HTTPException:
2151+
raise
21492152
except Exception as e:
21502153
log.exception(e)
21512154
raise HTTPException(
21522155
status_code=status.HTTP_400_BAD_REQUEST,
2153-
detail=error_detail(e, 'Error querying knowledge base'),
2156+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error querying knowledge base'),
21542157
)
21552158

21562159

@@ -2653,11 +2656,13 @@ async def search_query_with_semaphore(query):
26532656
'filenames': urls,
26542657
'loaded_count': len(docs),
26552658
}
2659+
except HTTPException:
2660+
raise
26562661
except Exception as e:
26572662
log.exception('Web search content loading failed')
26582663
raise HTTPException(
26592664
status.HTTP_400_BAD_REQUEST,
2660-
detail=error_detail(e, ERROR_MESSAGES.WEB_SEARCH_ERROR()),
2665+
detail=ERROR_MESSAGES.DEFAULT(e, ERROR_MESSAGES.WEB_SEARCH_ERROR()),
26612666
)
26622667

26632668

@@ -2732,11 +2737,13 @@ async def query_doc_handler(
27322737
k=form_data.k if form_data.k else config.TOP_K,
27332738
user=user,
27342739
)
2740+
except HTTPException:
2741+
raise
27352742
except Exception as e:
27362743
log.exception(e)
27372744
raise HTTPException(
27382745
status_code=status.HTTP_400_BAD_REQUEST,
2739-
detail=error_detail(e, 'Error querying knowledge base'),
2746+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error querying knowledge base'),
27402747
)
27412748

27422749

@@ -2798,11 +2805,13 @@ async def query_collection_handler(
27982805
k=form_data.k if form_data.k else config.TOP_K,
27992806
)
28002807

2808+
except HTTPException:
2809+
raise
28012810
except Exception as e:
28022811
log.exception(e)
28032812
raise HTTPException(
28042813
status_code=status.HTTP_400_BAD_REQUEST,
2805-
detail=error_detail(e, 'Error querying knowledge base'),
2814+
detail=ERROR_MESSAGES.DEFAULT(e, 'Error querying knowledge base'),
28062815
)
28072816

28082817

0 commit comments

Comments
 (0)