Repository navigation
refactor: simplify error message formatting in exception handling #7880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
85049ca
90a14ef
434a89f
e646626
15d8f16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -78,9 +78,7 @@ def is_valid( | |||||||||||||||||||||
| if raise_exception: | ||||||||||||||||||||||
| raise AppApiException( | ||||||||||||||||||||||
| ValidCode.valid_error.value, | ||||||||||||||||||||||
| gettext("Verification failed, please check whether the parameters are correct: {error}").format( | ||||||||||||||||||||||
| error=str(e) | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| str(e), | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| return False | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an analysis of the code changes and suggestions: 1. Potential Issues & IrregularitiesLoss of Context and User-Friendly Messaging
Security/Information Leakage
2. Optimization & Improvement Suggestions✅ Recommended Approach: Maintain Structure, Ensure SafetyInstead of returning just 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 HandlingIf 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
Final VerdictThe change is risky unless you have strict control over what 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 |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,9 +91,7 @@ def is_valid( | |
| if raise_exception: | ||
| raise AppApiException( | ||
| ValidCode.valid_error.value, | ||
| gettext("Verification failed, please check whether the parameters are correct: {error}").format( | ||
| error=str(e) | ||
| ), | ||
| str(e), | ||
| ) | ||
| return False | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the analysis of the code changes: Irregularities & Potential Issues
Optimization Suggestions
ConclusionThe change is not purely an optimization; it is a behavioral modification that reduces error message clarity and removes internationalization capabilities.
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the check of your code snippet:
1. Potential Issue: Model Classification Ambiguity
"bge" in model_id). This works for common cases (e.g.,bge-m3,bge-large) but may fail if:baai-bgevsbge).model_id.startswith("bge").2. Optimization / Robustness Suggestion
"bge"after"rerank"prevents misclassification ofbge-reranker-*. This is a good catch. ✅3. Code Style / Consistency
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.