mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 06:30:54 +08:00
Merge pull request #338 from Dev-next-gen/fix/llmpool-trimerr-utf8
fix(llmpool): keep trimmed provider errors on a rune boundary
This commit is contained in:
+6
-1
@@ -10,6 +10,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
)
|
||||
@@ -262,7 +263,11 @@ func isHardFailure(err error) bool {
|
||||
func trimErr(err error) string {
|
||||
s := strings.TrimSpace(strings.ReplaceAll(err.Error(), "\n", " "))
|
||||
if len(s) > 300 {
|
||||
s = s[:300] + "…"
|
||||
cut := 300
|
||||
for cut > 0 && !utf8.RuneStart(s[cut]) {
|
||||
cut--
|
||||
}
|
||||
s = s[:cut] + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
)
|
||||
@@ -368,3 +369,16 @@ func TestExpiredWindowIsClosed(t *testing.T) {
|
||||
t.Fatal("expired window should read as closed")
|
||||
}
|
||||
}
|
||||
|
||||
// A provider error body in Chinese is cut at 300 bytes; the cut must not split
|
||||
// a rune, or the breaker's last_error is rejected by PostgreSQL as invalid UTF-8.
|
||||
func TestTrimErrKeepsValidUTF8(t *testing.T) {
|
||||
err := errors.New("openai: status 503: " + strings.Repeat("服务暂时不可用,请稍后重试。", 10))
|
||||
got := trimErr(err)
|
||||
if !utf8.ValidString(got) {
|
||||
t.Fatalf("trimErr returned invalid UTF-8: %q", got)
|
||||
}
|
||||
if !strings.HasSuffix(got, "…") || len(got) > 300+len("…") {
|
||||
t.Fatalf("trimErr = %q (%d bytes), want at most 300 bytes plus …", got, len(got))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user