bugfix: SliceValidationError has two elements, but the first one is nil

This commit is contained in:
Rui Cheng 2023-07-24 23:17:22 +08:00
parent d4a64265f2
commit a475c6ec7c
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"),