-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
169 lines (153 loc) · 3.1 KB
/
multi.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2020 cloudeng LLC. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package errors
import (
"errors"
"fmt"
"strings"
"sync"
)
// New calls errors.New.
func New(m string) error {
return errors.New(m)
}
// Unwrap calls errors.Unwrap.
func Unwrap(err error) error {
return errors.Unwrap(err)
}
// Is calls errors.Is.
func Is(err, target error) bool {
return errors.Is(err, target)
}
// As calls errors.As.
func As(err error, target interface{}) bool {
return errors.As(err, target)
}
// M represents multiple errors. It is thread safe. Typical usage is:
//
// errs := errors.M{}
// ...
// errs.Append(err)
// ...
// return errs.Err()
type M struct {
mu sync.RWMutex
errs []error // GUARDED_BY(mu)
}
// Append appends the specified errors excluding nil values.
func (m *M) Append(errs ...error) {
m.mu.Lock()
defer m.mu.Unlock()
for _, err := range errs {
if err == nil {
continue
}
m.errs = append(m.errs, err)
}
}
// Unwrap implements errors.Unwrap. It returns the first stored error
// and then removes that error.
func (m *M) Unwrap() error {
m.mu.Lock()
defer m.mu.Unlock()
switch len(m.errs) {
case 0:
return nil
case 1:
err := m.errs[0]
m.errs = nil
return err
default:
err := m.errs[0]
n := make([]error, len(m.errs)-1)
copy(n, m.errs[1:])
m.errs = n
return err
}
}
// Is supports errors.Is.
func (m *M) Is(target error) bool {
m.mu.RLock()
defer m.mu.RUnlock()
for _, err := range m.errs {
if errors.Is(err, target) {
return true
}
}
return false
}
// As supports errors.As.
func (m *M) As(target interface{}) bool {
m.mu.RLock()
defer m.mu.RUnlock()
for _, err := range m.errs {
if errors.As(err, target) {
return true
}
}
return false
}
// Error implements error.error
func (m *M) Error() string {
m.mu.RLock()
defer m.mu.RUnlock()
l := len(m.errs)
switch l {
case 0:
return ""
case 1:
return m.errs[0].Error()
}
out := &strings.Builder{}
for i, err := range m.errs {
fmt.Fprintf(out, " --- %v of %v errors\n ", i+1, l)
out.WriteString(err.Error())
out.WriteString("\n")
}
return strings.TrimSuffix(out.String(), "\n")
}
// Format implements fmt.Formatter.Format.
func (m *M) Format(f fmt.State, c rune) {
format := "%" + string(c)
if !f.Flag('+') && !f.Flag('#') {
fmt.Fprintf(f, format, m.Error())
return
}
m.mu.RLock()
defer m.mu.RUnlock()
switch {
case f.Flag('+'):
format = "%+" + string(c)
case f.Flag('#'):
format = "%#" + string(c)
}
l := len(m.errs)
if l == 1 {
fmt.Fprintf(f, format, m.errs[0])
return
}
format += "\n"
for i, err := range m.errs {
fmt.Fprintf(f, " --- %v of %v errors\n ", i+1, l)
fmt.Fprintf(f, format, err)
}
}
// Err returns nil if m contains no errors, or itself otherwise.
func (m *M) Err() error {
m.mu.RLock()
defer m.mu.RUnlock()
if len(m.errs) == 0 {
return nil
}
return m
}
// Clone returns a new errors.M that contains the same errors as itself.
func (m *M) Clone() *M {
c := &M{}
m.mu.RLock()
defer m.mu.RUnlock()
c.errs = make([]error, len(m.errs))
copy(c.errs, m.errs)
return c
}