Skip to content

refactor: simplify error message formatting in exception handling - #7880

Closed
shaohuzhang1 wants to merge 5 commits into
v3from
pr@v3@fix_model_error_message
Closed

shaohuzhang1 wants to merge 5 commits into
v3from
pr@v3@fix_model_error_message

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

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

@wxg0103 wxg0103 closed this Nov 13, 2026
@wxg0103 wxg0103 reopened this Nov 13, 2026
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")):

Copy link
Copy Markdown
Contributor Author

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

  • 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-bge vs bge).
  • 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 of bge-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 ModelTypeConst imports 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 & 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 returns str(e).
  • Impact:
    • If the exception e does 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.
  • Internationalization (i18n): You removed the gettext() call. If str(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 error e may 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 if DEBUG=True or if the exception handling isn't strict. Ensure e is 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) ⚠️ Prefer retaining context unless e is perfectly formatted.
i18n Uses gettext No gettext wrapper ⚠️ Ensure 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the analysis of the code changes:

Irregularities & Potential Issues

  1. 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").
  2. 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, removing gettext breaks internationalization support for the generic error context if that existed in the frontend handling based on the structure.
  3. 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.

Optimization Suggestions

  1. Preserve User-Facing Context, but simplify formatting:
    If the goal was to remove verbose translation strings or fix a bug with gettext formatting, consider keeping a generic, non-translated prefix if possible, or ensure the client-side application handles the raw str(e) gracefully.

    If you must return only str(e), ensure your API consumers know the response body format has changed.

  2. If i18n is required, revert to using gettext but simplify the logic:

    if raise_exception:
        # Ensure translation exists for this message
        raise AppApiException(
            ValidCode.valid_error.value,
            gettext("Verification failed: {}").format(str(e))
        )
  3. If the old message was buggy (e.g., .format() causing issues with certain characters in str(e)):
    Use f-strings or %(x)s formatting 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>
Copilot AI requested a review from wxg0103 November 13, 2026 01:44
@wxg0103 wxg0103 closed this Nov 13, 2026
@wxg0103 wxg0103 reopened this Nov 13, 2026
@wxg0103 wxg0103 closed this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants