-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatter.go
37 lines (31 loc) · 996 Bytes
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package errortree
import (
"fmt"
"sort"
"strings"
)
// Formatter defines the Formatter type
// This function can expected that the provided map contains a flattened map of all Errors
type Formatter func(map[string]error) string
// SimpleFormatter provides a simple Formatter which returns a message indicating
// how many Errors occurred and details for every error.
// The reported Errors are sorted alphabetically by key.
func SimpleFormatter(errorMap map[string]error) string {
wrappedErrors := make([]string, len(errorMap))
pluralSuffix := ""
if len(errorMap) != 1 {
pluralSuffix = "s"
}
// Sort the keys
keys := make([]string, 0, len(errorMap))
for key := range errorMap {
keys = append(keys, key)
}
sort.Strings(keys)
// Construct the individual messages
for i, key := range keys {
wrappedErrors[i] = "* " + key + ": " + errorMap[key].Error()
}
return fmt.Sprintf("%d error%s occurred:\n\n%s", len(errorMap), pluralSuffix,
strings.Join(wrappedErrors, "\n"))
}