File: multierr.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (34 lines) | stat: -rw-r--r-- 1,105 bytes parent folder | download | duplicates (2)
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
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...)
	}
}