Merge pull request #5031 from keaipiao/fix/easypay-error-utf8

fix(payment): preserve UTF-8 in EasyPay errors
This commit is contained in:
Wesley Liddick
2026-08-06 14:10:41 +08:00
committed by GitHub
2 changed files with 19 additions and 1 deletions
+6 -1
View File
@@ -15,6 +15,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/Wei-Shaw/sub2api/internal/payment"
)
@@ -477,7 +478,11 @@ func summarizeEasyPayResponse(body []byte) string {
return "<empty>"
}
if len(summary) > maxEasypayErrorSummary {
return summary[:maxEasypayErrorSummary] + "..."
truncated := summary[:maxEasypayErrorSummary]
for len(truncated) > 0 && !utf8.ValidString(truncated) {
truncated = truncated[:len(truncated)-1]
}
return truncated + "..."
}
return summary
}
@@ -7,6 +7,7 @@ import (
"net/url"
"strings"
"testing"
"unicode/utf8"
"github.com/Wei-Shaw/sub2api/internal/payment"
)
@@ -179,6 +180,18 @@ func TestEasyPayRefundResponseErrors(t *testing.T) {
}
}
func TestSummarizeEasyPayResponsePreservesUTF8(t *testing.T) {
t.Parallel()
summary := summarizeEasyPayResponse([]byte(strings.Repeat("错", 171)))
if !utf8.ValidString(summary) {
t.Fatalf("summarizeEasyPayResponse returned invalid UTF-8: %q", summary)
}
if !strings.HasSuffix(summary, "...") {
t.Fatalf("summarizeEasyPayResponse() = %q, want truncated suffix", summary)
}
}
func TestEasyPayCustomMethodsUseConfiguredUpstreamType(t *testing.T) {
t.Parallel()