Files
ttstd-infinite-canvas/backend/internal/service/provider_error.go

65 lines
1.6 KiB
Go

package service
import (
"fmt"
"strings"
)
const contentModerationErrorCode = "sensitive_words_detected"
const contentModerationRetryMessage = "内容审核未通过,请修改提示词后重新生成;原任务不能直接重试"
// 只提取供应商明确返回的错误码和短消息,避免把完整响应或用户输入复制到调用日志。
func providerFailureDetails(payload map[string]any) (string, string) {
candidates := make([]map[string]any, 0, 3)
for _, key := range []string{"error", "data"} {
if nested, ok := payload[key].(map[string]any); ok {
candidates = append(candidates, nested)
}
}
// 内层通常是供应商业务错误,外层 code 可能只是 HTTP 包装码。
candidates = append(candidates, payload)
code := ""
message := ""
for _, candidate := range candidates {
if code == "" {
code = normalizedProviderErrorCode(candidate["code"])
}
if message == "" {
message = strings.TrimSpace(stringField(candidate, "message"))
}
}
return code, truncateRunes(message, 500)
}
func normalizedProviderErrorCode(value any) string {
var code string
switch current := value.(type) {
case string:
code = current
case fmt.Stringer:
code = current.String()
case float64:
if current != 0 {
code = fmt.Sprintf("%g", current)
}
case int:
if current != 0 {
code = fmt.Sprintf("%d", current)
}
case int64:
if current != 0 {
code = fmt.Sprintf("%d", current)
}
}
code = strings.TrimSpace(code)
if code == "0" {
return ""
}
return truncateRunes(code, 80)
}
func isContentModerationFailure(value string) bool {
return strings.Contains(strings.ToLower(value), contentModerationErrorCode)
}