fix(v2): webhook 截断 UTF-8 安全 + 投递门禁查库失败落日志(与未付区分)

- MarkFailed: truncateUTF8 避免截断中点 UTF-8 rune, last_error 安全 <=255B
- notifier truncate: 同上, 投递失败消息截断 UTF-8 安全
- deliverOne: 拆分 orderPaid 错误分支 — DB 错误落日志 [webhook] 投递门禁查单失败,与"订单未付"(静默)区分
- test: MarkFailed with long Chinese string, 验证 stored last_error 为有效 UTF-8 且 <=255B

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
This commit is contained in:
wangjia
2026-07-10 11:31:16 +08:00
parent 3f5e44581f
commit 63af44bfe1
3 changed files with 64 additions and 6 deletions
+14 -3
View File
@@ -2,6 +2,7 @@ package store
import (
"fmt"
"unicode/utf8"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -51,12 +52,22 @@ func (s *WebhookStore) MarkDelivered(id uint64) error {
return nil
}
// truncateUTF8 safely truncates a string to n bytes without splitting UTF-8 runes.
func truncateUTF8(s string, n int) string {
if len(s) <= n {
return s
}
s = s[:n]
for len(s) > 0 && !utf8.ValidString(s) {
s = s[:len(s)-1]
}
return s
}
// MarkFailed increments attempts and records the last error, leaving the row
// undelivered for the next retry sweep.
func (s *WebhookStore) MarkFailed(id uint64, errMsg string) error {
if len(errMsg) > 255 {
errMsg = errMsg[:255]
}
errMsg = truncateUTF8(errMsg, 255)
if err := s.db.Model(&model.WebhookDelivery{}).Where("id = ?", id).
Updates(map[string]any{
"attempts": gorm.Expr("attempts + 1"),