errors.*MultiError.Unwrap   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 4
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
nop 0
1
package errors
2
3
import (
4
	"fmt"
5
	"strings"
6
	"sync"
7
)
8
9
type MultiError struct {
10
	errs  []error
11
	mutex sync.Mutex
12
}
13
14
func NewMulti() *MultiError {
15 2
	return &MultiError{}
16
}
17
18
func (e *MultiError) Error() string {
19 3
	if len(e.errs) == 1 {
20 1
		return e.errs[0].Error()
21
	}
22
23 2
	msg := make([]string, len(e.errs))
24 2
	for i, err := range e.errs {
25 4
		msg[i] = err.Error()
26
	}
27
28 2
	return fmt.Sprintf("%d errors occurred:\n %s", len(e.errs), strings.Join(msg, "\n "))
29
}
30
31
func (e *MultiError) GoString() string {
32
	return fmt.Sprintf("%#v", e.errs)
33
}
34
35
func (e *MultiError) Unwrap() []error {
36 13
	if e == nil {
37
		return nil
38
	}
39
40 13
	return e.errs
41
}
42
43
func (e *MultiError) Append(err error) {
44 1003
	if err == nil {
45 1
		return
46
	}
47
48 1002
	e.mutex.Lock()
49 1002
	defer e.mutex.Unlock()
50
51 1002
	e.errs = append(e.errs, err)
52
}
53
54
func (e *MultiError) ErrorOrNil() error {
55 2
	if len(e.errs) == 0 {
56 1
		return nil
57
	}
58
59 1
	return e
60
}
61