Skip to content

Commit

Permalink
fix: reduce verbosity and whitespace of multierror
Browse files Browse the repository at this point in the history
Add a customised default implementation of the multierror format func
that is less verbose for single error multierrors (return them without
the unnecessary "1 error occured:" prefix) and also condenses the output
by removing one of the newline separators from between multiple errors
in the output.
  • Loading branch information
dnwe committed Feb 27, 2022
1 parent abc23a2 commit 4862c46
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sarama
import (
"errors"
"fmt"
"strings"

"github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -63,8 +64,23 @@ var ErrReassignPartitions = errors.New("failed to reassign partitions for topic"
// ErrDeleteRecords is the type of error returned when fail to delete the required records
var ErrDeleteRecords = errors.New("kafka server: failed to delete records")

// The formatter used to format multierrors
var MultiErrorFormat multierror.ErrorFormatFunc
// MultiErrorFormat specifies the formatter applied to format multierrors. The
// default implementation is a consensed version of the hashicorp/go-multierror
// default one
var MultiErrorFormat multierror.ErrorFormatFunc = func(es []error) string {
if len(es) == 1 {
return es[0].Error()
}

points := make([]string, len(es))
for i, err := range es {
points[i] = fmt.Sprintf("* %s", err)
}

return fmt.Sprintf(
"%d errors occurred:\n\t%s\n",
len(es), strings.Join(points, "\n\t"))
}

type sentinelError struct {
sentinel error
Expand Down

0 comments on commit 4862c46

Please sign in to comment.