Source file
src/errors/join.go
1
2
3
4
5 package errors
6
7 import (
8 "unsafe"
9 )
10
11
12
13
14
15
16
17
18
19 func Join(errs ...error) error {
20 n := 0
21 for _, err := range errs {
22 if err != nil {
23 n++
24 }
25 }
26 if n == 0 {
27 return nil
28 }
29 e := &joinError{
30 errs: make([]error, 0, n),
31 }
32 for _, err := range errs {
33 if err != nil {
34 e.errs = append(e.errs, err)
35 }
36 }
37 return e
38 }
39
40 type joinError struct {
41 errs []error
42 }
43
44 func (e *joinError) Error() string {
45
46
47 if len(e.errs) == 1 {
48 return e.errs[0].Error()
49 }
50
51 b := []byte(e.errs[0].Error())
52 for _, err := range e.errs[1:] {
53 b = append(b, '\n')
54 b = append(b, err.Error()...)
55 }
56
57 return unsafe.String(&b[0], len(b))
58 }
59
60 func (e *joinError) Unwrap() []error {
61 return e.errs
62 }
63
View as plain text