mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-22 21:28:12 +02:00
feat(chat): enhance error formatting to include optional error codes for better frontend handling
This commit is contained in:
parent
fa6a09197e
commit
901de33684
4 changed files with 97 additions and 58 deletions
|
|
@ -565,20 +565,24 @@ class VercelStreamingService:
|
|||
# Error Part
|
||||
# =========================================================================
|
||||
|
||||
def format_error(self, error_text: str) -> str:
|
||||
def format_error(self, error_text: str, error_code: str | None = None) -> str:
|
||||
"""
|
||||
Format an error message.
|
||||
|
||||
Args:
|
||||
error_text: The error message text
|
||||
error_code: Optional machine-readable error code for frontend branching
|
||||
|
||||
Returns:
|
||||
str: SSE formatted error part
|
||||
|
||||
Example output:
|
||||
data: {"type":"error","errorText":"Something went wrong"}
|
||||
data: {"type":"error","errorText":"Something went wrong","errorCode":"SOME_CODE"}
|
||||
"""
|
||||
return self._format_sse({"type": "error", "errorText": error_text})
|
||||
payload: dict[str, str] = {"type": "error", "errorText": error_text}
|
||||
if error_code:
|
||||
payload["errorCode"] = error_code
|
||||
return self._format_sse(payload)
|
||||
|
||||
# =========================================================================
|
||||
# Tool Parts
|
||||
|
|
|
|||
|
|
@ -1581,7 +1581,8 @@ async def stream_new_chat(
|
|||
)
|
||||
else:
|
||||
yield streaming_service.format_error(
|
||||
"Premium tokens exhausted. Buy more tokens to continue with this model, or switch to a free model."
|
||||
"Buy more tokens to continue with this model, or switch to a free model.",
|
||||
error_code="PREMIUM_QUOTA_EXHAUSTED",
|
||||
)
|
||||
yield streaming_service.format_done()
|
||||
return
|
||||
|
|
@ -2348,7 +2349,8 @@ async def stream_resume_chat(
|
|||
)
|
||||
else:
|
||||
yield streaming_service.format_error(
|
||||
"Premium tokens exhausted. Buy more tokens to continue with this model, or switch to a free model."
|
||||
"Buy more tokens to continue with this model, or switch to a free model.",
|
||||
error_code="PREMIUM_QUOTA_EXHAUSTED",
|
||||
)
|
||||
yield streaming_service.format_done()
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue