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
+13 -3
View File
@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
@@ -69,7 +70,11 @@ func (n *Notifier) DeliverPending(limit int) (int, error) {
func (n *Notifier) deliverOne(d *store.WebhookDeliveryRow) bool {
// 门禁:订单未付不投递(不计失败,等 settle 翻转后自然放行)。
paid, err := n.orderPaid(d.OutTradeNo)
if err != nil || !paid {
if err != nil {
log.Printf("[webhook] 投递门禁查单失败 %s: %v", d.OutTradeNo, err)
return false
}
if !paid {
return false
}
cfg, found := n.bizConfig(d.BizSystem)
@@ -109,9 +114,14 @@ func (n *Notifier) deliverOne(d *store.WebhookDeliveryRow) bool {
return false
}
// truncate safely truncates a string to n bytes without splitting UTF-8 runes.
func truncate(s string, n int) string {
if len(s) > n {
return s[:n]
if len(s) <= n {
return s
}
s = s[:n]
for len(s) > 0 && !utf8.ValidString(s) {
s = s[:len(s)-1]
}
return s
}