Merge a475c6ec7c568ef6e81ac316123dc0b429c640ea into 0397e5e0c0f8f8176c29f7edd8f1bff8e45df780

This commit is contained in:
Laotree 2024-04-12 18:56:54 +08:00 committed by GitHub
commit 7fbb4d9dfd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 7 deletions

View File

@ -28,15 +28,12 @@ func (err SliceValidationError) Error() string {
return ""
default:
var b strings.Builder
if err[0] != nil {
fmt.Fprintf(&b, "[%d]: %s", 0, err[0].Error())
}
if n > 1 {
for i := 1; i < n; i++ {
if err[i] != nil {
for i := 0; i < n; i++ {
if err[i] != nil {
if b.Len() > 0 {
b.WriteString("\n")
fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())
}
fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())
}
}
return b.String()

View File

@ -25,6 +25,13 @@ func TestSliceValidationError(t *testing.T) {
},
"[0]: first error\n[1]: second error",
},
{"has two elements, but the first one is nil",
SliceValidationError{
nil,
errors.New("first error at second place"),
},
"[1]: first error at second place",
},
{"has many elements",
SliceValidationError{
errors.New("first error"),