fix(handlers): preserve upstream response logs before duplicate detection

This commit is contained in:
hkfires
2025-12-28 22:35:36 +08:00
parent 3ca5fb1046
commit a95428f204

View File

@@ -618,28 +618,21 @@ func (h *BaseAPIHandler) WriteErrorResponse(c *gin.Context, msg *interfaces.Erro
} }
body := BuildErrorResponseBody(status, errText) body := BuildErrorResponseBody(status, errText)
// Check if this error was already recorded by the executor (to avoid duplicate logging) // Append first to preserve upstream response logs, then drop duplicate payloads if already recorded.
// The executor logs raw error text, so match errText before falling back to the JSON body. var previous []byte
shouldAppend := true
if existing, exists := c.Get("API_RESPONSE"); exists { if existing, exists := c.Get("API_RESPONSE"); exists {
if existingBytes, ok := existing.([]byte); ok && len(existingBytes) > 0 { if existingBytes, ok := existing.([]byte); ok && len(existingBytes) > 0 {
trimmedErrText := strings.TrimSpace(errText) previous = bytes.Clone(existingBytes)
if trimmedErrText != "" && bytes.Contains(existingBytes, []byte(trimmedErrText)) {
// Error already logged by executor, skip appending
shouldAppend = false
} else {
trimmedBody := bytes.TrimSpace(body)
if len(trimmedBody) > 0 && bytes.Contains(existingBytes, trimmedBody) {
// Error already logged by executor, skip appending
shouldAppend = false
} }
} }
}
}
if shouldAppend {
// Use appendAPIResponse to preserve any previously captured API response data
// (such as formatted upstream response logs from logging_helpers.go)
appendAPIResponse(c, body) appendAPIResponse(c, body)
trimmedErrText := strings.TrimSpace(errText)
trimmedBody := bytes.TrimSpace(body)
if len(previous) > 0 {
if (trimmedErrText != "" && bytes.Contains(previous, []byte(trimmedErrText))) ||
(len(trimmedBody) > 0 && bytes.Contains(previous, trimmedBody)) {
c.Set("API_RESPONSE", previous)
}
} }
if !c.Writer.Written() { if !c.Writer.Written() {