Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer formatting when only one error is returned #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ type ErrorFormatFunc func([]error) string

// ListFormatFunc is a basic formatter that outputs the number of errors
// that occurred along with a bullet point list of the errors.
//
// If only one error is within the errror slice,
// the formatting should be deferred to the one and only error present
func ListFormatFunc(es []error) string {
if len(es) == 1 {
return fmt.Sprintf("1 error occurred:\n\t* %s\n\n", es[0])
// Formatting should be deferred to the single error present
return es[0].Error()
}

points := make([]string, len(es))
Expand Down
40 changes: 27 additions & 13 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@ import (
)

func TestListFormatFuncSingle(t *testing.T) {
expected := `1 error occurred:
* foo

`

errors := []error{
errors.New("foo"),
}

actual := ListFormatFunc(errors)
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
t.Run("Flat", func(t *testing.T) {
expected := `foo`

errors := []error{
errors.New("foo"),
}

actual := ListFormatFunc(errors)
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
})

t.Run("Nested", func(t *testing.T) {
expected := `foo`

nestedErrors := &Error{
Errors: []error{
&Error{Errors: []error{errors.New("foo")}},
},
}

actual := ListFormatFunc(nestedErrors.Errors)
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
})
}

func TestListFormatFuncMultiple(t *testing.T) {
Expand Down