Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions go/action_kit_commons/network/netfault/batch_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,23 @@ func (t *batchError) Error() string {
}

func (t *batchErrors) Error() string {
occurrences := make(map[string]int)
for _, err := range t.Errors {
occurrences[err.Msg]++
}

var sb strings.Builder
sb.WriteString(fmt.Sprintf("Command failed %s\n", strings.Join(t.Cmd, " ")))
reported := make(map[string]bool)
for _, err := range t.Errors {
if reported[err.Msg] {
continue
}
reported[err.Msg] = true
sb.WriteString(err.Error())
if count := occurrences[err.Msg]; count > 1 {
sb.WriteString(fmt.Sprintf(" (and %d more)", count-1))
}
sb.WriteString("\n")
}
return sb.String()
Expand Down
42 changes: 41 additions & 1 deletion go/action_kit_commons/network/netfault/batch_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ Command failed -:2
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:3
`
exampleErrorRepeated := `Error: NLM_F_REPLACE needed to override.
Command failed -:1
Error: Failed to find specified qdisc.
Command failed -:2
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:3
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:4
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:5
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:6
`

tests := []struct {
Expand All @@ -56,12 +73,35 @@ Command failed -:3
},
assert: func(t assert.TestingT, err error, message string) {
assert.Equal(t, 3, len(err.(*batchErrors).Errors))
assert.Equal(t, exampleError, strings.TrimPrefix(err.Error(), "Command failed test -b -\n"))
assert.Equal(t, `Error: Exclusivity flag on, cannot modify.
Command failed -:1 (and 1 more)
RTNETLINK answers: File exists
Command failed -:2
`, strings.TrimPrefix(err.Error(), "Command failed test -b -\n"))
assert.Equal(t, "Error: Exclusivity flag on, cannot modify.", err.(*batchErrors).Errors[0].Msg)
assert.Equal(t, "RTNETLINK answers: File exists", err.(*batchErrors).Errors[1].Msg)
assert.Equal(t, "Error: Exclusivity flag on, cannot modify.", err.(*batchErrors).Errors[2].Msg)
},
},
{
name: "should report repeated errors only once",
args: args{
cmd: []string{"tc", "-force", "-batch", "-"},
r: strings.NewReader(exampleErrorRepeated),
},
assert: func(t assert.TestingT, err error, message string) {
assert.Equal(t, 6, len(err.(*batchErrors).Errors))
assert.Equal(t, `Command failed tc -force -batch -
Error: NLM_F_REPLACE needed to override.
Command failed -:1
Error: Failed to find specified qdisc.
Command failed -:2
Error: Parent Qdisc doesn't exists.
We have an error talking to the kernel
Command failed -:3 (and 3 more)
`, err.Error())
},
},
{
name: "should add kernel module hint",
args: args{
Expand Down
Loading