Repository navigation
refactor: simplify error message formatting in exception handling - #7880
shaohuzhang1 wants to merge 5 commits into
Conversation
… search functionality
…config in request body
| return ModelTypeConst.EMBEDDING | ||
| if any(k in model_id for k in ("whisper", "asr", "transcribe", "paraformer")): | ||
| return ModelTypeConst.STT | ||
| if any(k in model_id for k in ("tts", "speech", "voice", "cosyvoice", "sambert")): |
There was a problem hiding this comment.
Here is the check of your code snippet:
1. Potential Issue: Model Classification Ambiguity
- Problem: The logic relies on string matching (
"bge" in model_id). This works for common cases (e.g.,bge-m3,bge-large) but may fail if:- A non-BGE embedding model happens to contain “bge” in its name (unlikely, but possible in custom/edge cases).
- A new BGE variant changes naming conventions slightly (e.g.,
baai-bgevsbge).
- Suggestion: Consider using a more deterministic mapping or prefix check if model IDs are predictable, e.g.,
model_id.startswith("bge").
2. Optimization / Robustness Suggestion
- Order matters: Your comment correctly notes that placing
"bge"after"rerank"prevents misclassification ofbge-reranker-*. This is a good catch. ✅ - Consider explicit handling: If you maintain a list of known BGE models, matching against exact prefixes or a curated dict could be safer than substring checks.
3. Code Style / Consistency
- The use of Chinese comments is fine for internal teams, but ensure consistency across the codebase if internationalization is a concern.
- Ensure
ModelTypeConstimports and types are consistent.
Final Verdict
The change is functionally sound given typical Hugging Face/BAAI model naming conventions. Just ensure no future model violates the assumption that *bge* (excluding rerankers) is always an embedding model.
| str(e), | ||
| ) | ||
| return False | ||
|
|
There was a problem hiding this comment.
Here is an analysis of the code changes and suggestions:
1. Potential Issues & Irregularities
Loss of Context and User-Friendly Messaging
- Issue: The original code provided a generic, localized message (
"Verification failed...") and appended the specific error details. The new code simply returnsstr(e). - Impact:
- If the exception
edoes not have a clear, human-readable message (e.g., it's a stack trace fragment or a technical detail like'key'), the user/API client will see useless information. - You lose the ability to distinguish between "valid format but wrong value" vs. "internal system error" in the response string.
- If the exception
- Internationalization (i18n): You removed the
gettext()call. Ifstr(e)contains text from a library that might be localized differently or is in English, you are no longer ensuring consistent localization for the wrapper message. The raw erroremay or may not be translated depending on its origin.
Security/Information Leakage
- Risk: Returning
str(e)directly can sometimes leak internal implementation details, paths, or query strings in debugging environments ifDEBUG=Trueor if the exception handling isn't strict. Ensureeis sanitized before exposing it to the end-user.
2. Optimization & Improvement Suggestions
✅ Recommended Approach: Maintain Structure, Ensure Safety
Instead of returning just str(e), consider keeping a structured response while being concise:
if raise_exception:
# Option A: Keep context + localizable prefix
msg = gettext("Verification failed: {error}").format(error=str(e))
raise AppApiException(ValidCode.valid_error.value, msg)
# OR
# Option B: If str(e) is ALWAYS guaranteed to be a perfect user-facing message
raise AppApiException(ValidCode.valid_error.value, str(e))🔧 Better Practice: Custom Exception Handling
If str(e) varies widely, ensure your exceptions inherit from a base class that guarantees a .message attribute suitable for display:
class ValidationException(Exception):
def __init__(self, message: str):
self.message = message # Guaranteed user-safe
super().__init__(self.message)Then in your code:
raise AppApiException(
ValidCode.valid_error.value,
e.message # Clean, pre-formatted, safe
)3. Summary of Recommendations
| Concern | Original Code | New Code | Recommendation |
|---|---|---|---|
| Clarity | High ("Verification failed...") | Low (depends entirely on e) |
e is perfectly formatted. |
| i18n | Uses gettext |
No gettext wrapper |
e itself is localized, or keep the wrapper. |
| Robustness | Consistent format | Variable format | ✅ Use a custom exception with a .message property. |
| Conciseness | Verbose | Concise | ✅ Good if e is already a clean, short, user-friendly string. |
Final Verdict
The change is risky unless you have strict control over what e represents. If e comes from third-party libraries or deep validation logic, str(e) may be too technical for users.
Safer refactor:
if raise_exception:
# Truncate if necessary to avoid huge payloads
error_msg = str(e).strip()[:200]
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed: {error}").format(error=error_msg)
)
return False| str(e), | ||
| ) | ||
| return False | ||
|
|
There was a problem hiding this comment.
Here is the analysis of the code changes:
Irregularities & Potential Issues
-
Loss of Context/Error Message Clarity:
- Before: The error message was
"Verification failed, please check whether the parameters are correct: {error}". This provided clear context to the API consumer about why the validation failed generally and included specific details. - After: The exception only contains
str(e), which is just the raw exception message from the underlying validator. This removes the standardized "Verification failed..." prefix. - Impact: Clients relying on specific string matching or expecting a consistent format for user-facing errors may break or provide poor UX (e.g., showing a cryptic technical constraint violation instead of "Please check parameters").
- Before: The error message was
-
Internationalization (i18n) Regression:
- The original code used
gettext()to translate the static part of the message ("Verification failed..."). - The new code removes this. While the dynamic
str(e)likely wasn't translated, removinggettextbreaks internationalization support for the generic error context if that existed in the frontend handling based on the structure.
- The original code used
-
Security/Information Leakage:
- Exposing raw
str(e)might leak internal implementation details (e.g., database-specific constraints, library internals) that could be helpful to attackers, whereas the previous message wrapped it in a generic validation failure.
- Exposing raw
Optimization Suggestions
-
Preserve User-Facing Context, but simplify formatting:
If the goal was to remove verbose translation strings or fix a bug withgettextformatting, consider keeping a generic, non-translated prefix if possible, or ensure the client-side application handles the rawstr(e)gracefully.If you must return only
str(e), ensure your API consumers know the response body format has changed. -
If i18n is required, revert to using
gettextbut simplify the logic:if raise_exception: # Ensure translation exists for this message raise AppApiException( ValidCode.valid_error.value, gettext("Verification failed: {}").format(str(e)) )
-
If the old message was buggy (e.g.,
.format()causing issues with certain characters instr(e)):
Use f-strings or%(x)sformatting which are often safer/more efficient than.format()with concatenated translated strings. However, never drop the context entirely unless intentional.
Conclusion
The change is not purely an optimization; it is a behavioral modification that reduces error message clarity and removes internationalization capabilities.
- ✅ Acceptable if the product requirements explicitly state that developers/clients should handle raw Python/Validator exceptions directly and no localized/generic messaging is needed.
- ❌ Regression if user-friendly or standardized API error messages are expected.
Co-authored-by: wxg0103 <46886316+wxg0103@users.noreply.github.com>
refactor: update string filtering to be case-insensitive for improved search functionality
refactor: add client parameter handling to prevent sending sensitive config in request body
refactor: simplify error message formatting in exception handling