-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2365 from mtrmac/multierr
Add a helper for formatting multiple errors
- Loading branch information
Showing
10 changed files
with
118 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package multierr | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Format creates an error value from the input array (which should not be empty) | ||
// If the input contains a single error value, it is returned as is. | ||
// If there are multiple, they are formatted as a multi-error (with Unwrap() []error) with the provided initial, separator, and ending strings. | ||
// | ||
// Typical usage: | ||
// | ||
// var errs []error | ||
// // … | ||
// errs = append(errs, …) | ||
// // … | ||
// if errs != nil { return multierr.Format("Failures doing $FOO", "\n* ", "", errs)} | ||
func Format(first, middle, last string, errs []error) error { | ||
switch len(errs) { | ||
case 0: | ||
return fmt.Errorf("internal error: multierr.Format called with 0 errors") | ||
case 1: | ||
return errs[0] | ||
default: | ||
// We have to do this — and this function only really exists — because fmt.Errorf(format, errs...) is invalid: | ||
// []error is not a valid parameter to a function expecting []any | ||
anyErrs := make([]any, 0, len(errs)) | ||
for _, e := range errs { | ||
anyErrs = append(anyErrs, e) | ||
} | ||
return fmt.Errorf(first+"%w"+strings.Repeat(middle+"%w", len(errs)-1)+last, anyErrs...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package multierr | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type errA struct{} | ||
|
||
func (errA) Error() string { return "A" } | ||
|
||
func TestFormat(t *testing.T) { | ||
errB := errors.New("B") | ||
|
||
// Single-item Format preserves the error type | ||
res := Format("[", ",", "]", []error{errA{}}) | ||
assert.Equal(t, "A", res.Error()) | ||
var aTarget errA | ||
assert.ErrorAs(t, res, &aTarget) | ||
|
||
// Single-item Format preserves the error identity | ||
res = Format("[", ",", "]", []error{errB}) | ||
assert.Equal(t, "B", res.Error()) | ||
assert.ErrorIs(t, res, errB) | ||
|
||
// Multi-item Format preserves both | ||
res = Format("[", ",", "]", []error{errA{}, errB}) | ||
assert.Equal(t, "[A,B]", res.Error()) | ||
assert.ErrorAs(t, res, &aTarget) | ||
assert.ErrorIs(t, res, errB) | ||
|
||
// This is invalid, but make sure we don’t misleadingly suceeed | ||
res = Format("[", ",", "]", []error{}) | ||
assert.Error(t, res) | ||
res = Format("[", ",", "]", nil) | ||
assert.Error(t, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.