Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion services/log_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,20 @@ def _image_error_response(exc: Exception) -> JSONResponse:
return openai_error_response(message, 502)


def _sanitize_error_message(message: str) -> str:
import re

text = str(message or "").strip()
lower = text.lower()
if any(item in lower for item in ("backend-api/", "status=", "body=", "chatgpt.com", "upstreamhttperror")):
return "An internal error occurred. Please try again later."
if "@" in text and re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', text):
return "An internal error occurred. Please try again later."
return text or "An internal error occurred. Please try again later."


def _protocol_error_response(exc: Exception, status_code: int, sse: str) -> JSONResponse:
message = str(exc)
message = _sanitize_error_message(str(exc))
if sse == "anthropic":
return anthropic_error_response(message, status_code)
return openai_error_response(message, status_code)
Expand Down
12 changes: 10 additions & 2 deletions services/protocol/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@ def to_openai_error(self) -> dict[str, Any]:
}


def _mask_token(token: str, visible: int = 8) -> str:
if len(token) <= visible:
return "***"
return f"***{token[-visible:]}"


def public_image_error_message(message: str) -> str:
text = str(message or "").strip()
lower = text.lower()
if any(item in lower for item in ("backend-api/", "status=", "body=", "chatgpt.com", "upstreamhttperror")):
return "The image generation request failed. Please try again later."
if "@" in text and re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', text):
return "The image generation request failed. Please try again later."
return text or "The image generation request failed. Please try again later."


Expand Down Expand Up @@ -746,7 +754,7 @@ def stream_image_outputs_with_pool(request: ConversationRequest) -> Iterator[Ima
exc.account_email = account_email
logger.warning({
"event": "image_stream_generation_error",
"request_token": token,
"request_token": _mask_token(token),
"account_email": account_email,
"error": str(exc),
})
Expand All @@ -756,7 +764,7 @@ def stream_image_outputs_with_pool(request: ConversationRequest) -> Iterator[Ima
last_error = str(exc)
logger.warning({
"event": "image_stream_fail",
"request_token": token,
"request_token": _mask_token(token),
"account_email": account_email,
"error": last_error,
})
Expand Down