File: multierror.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (46 lines) | stat: -rw-r--r-- 818 bytes parent folder | download | duplicates (6)
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
38
39
40
41
42
43
44
45
46
package multierror

import (
	"strings"
)

// Join is a drop-in replacement for errors.Join with better formatting.
func Join(errs ...error) error {
	n := 0
	for _, err := range errs {
		if err != nil {
			n++
		}
	}
	if n == 0 {
		return nil
	}
	e := &joinError{
		errs: make([]error, 0, n),
	}
	for _, err := range errs {
		if err != nil {
			e.errs = append(e.errs, err)
		}
	}
	return e
}

type joinError struct {
	errs []error
}

func (e *joinError) Error() string {
	if len(e.errs) == 1 {
		return strings.TrimSpace(e.errs[0].Error())
	}
	stringErrs := make([]string, 0, len(e.errs))
	for _, subErr := range e.errs {
		stringErrs = append(stringErrs, strings.Replace(subErr.Error(), "\n", "\n\t", -1))
	}
	return "* " + strings.Join(stringErrs, "\n* ")
}

func (e *joinError) Unwrap() []error {
	return e.errs
}