-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
256 lines (213 loc) · 5.69 KB
/
error.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package goip
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/pchchv/goip/address_error"
)
type addressError struct {
key string // to look up the error message
str string // an optional string with the address
}
func (a *addressError) Error() string {
return getStr(a.str) + lookupStr("ipaddress.address.error") + " " + lookupStr(a.key)
}
// GetKey can be used to internationalize error strings in the goip library.
// The list of keys and their English translations are listed in IPAddressResources.properties.
// Use your own method to map keys to your translations.
// One such method is golang.org/x/text which provides language tags
// (https://pkg.go.dev/golang.org/x/text/language#Tag),
// that can then be mapped to catalogs, each catalog being a translation list for the set of keys presented here.
// In the code, you specify the language key to use the right catalog.
// You can use the gotext tool to integrate these translations into your application.
func (a *addressError) GetKey() string {
return a.key
}
func getStr(str string) (res string) {
if len(str) > 0 {
res = str + " "
}
return
}
type incompatibleAddressError struct {
addressError
}
type sizeMismatchError struct {
incompatibleAddressError
}
type addressValueError struct {
addressError
val int
}
type mergedError struct {
address_error.AddressError
merged []address_error.AddressError
}
func (a *mergedError) GetMerged() []address_error.AddressError {
return a.merged
}
type addressStringError struct {
addressError
}
type addressStringNestedError struct {
addressStringError
nested address_error.AddressStringError
}
func (a *addressStringNestedError) Error() string {
return a.addressError.Error() + ": " + a.nested.Error()
}
type addressStringIndexError struct {
addressStringError
index int // byte index location in string of the error
}
func (a *addressStringIndexError) Error() string {
return lookupStr("ipaddress.address.error") + " " + lookupStr(a.key) + " " + strconv.Itoa(a.index)
}
type hostNameError struct {
addressError
}
// GetAddrError returns the nested address error which is nil for a host name error
func (a *hostNameError) GetAddrError() address_error.AddressError {
return nil
}
func (a *hostNameError) Error() string {
return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + lookupStr(a.key)
}
type hostNameNestedError struct {
hostNameError
nested error
}
type hostNameIndexError struct {
hostNameError
index int
}
func (a *hostNameIndexError) Error() string {
return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + lookupStr(a.key) + " " + strconv.Itoa(a.index)
}
type hostAddressNestedError struct {
hostNameIndexError
nested address_error.AddressError
}
// GetAddrError returns the nested address error
func (a *hostAddressNestedError) GetAddrError() address_error.AddressError {
return a.nested
}
func (a *hostAddressNestedError) Error() string {
if a.hostNameIndexError.key != "" {
return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + a.hostNameIndexError.Error() + " " + a.nested.Error()
}
return getStr(a.str) + lookupStr("ipaddress.host.error") + " " + a.nested.Error()
}
type wrappedErr struct {
cause error // root cause
err error // wrapper
str string
}
func (wrappedErr *wrappedErr) Error() string {
str := wrappedErr.str
if len(str) > 0 {
return str
}
str = wrappedErr.err.Error() + ": " + wrappedErr.cause.Error()
wrappedErr.str = str
return str
}
type mergedErr struct {
mergedErrs []error
str string
}
func (merged *mergedErr) Error() (str string) {
str = merged.str
if len(str) > 0 {
return
}
mergedErrs := merged.mergedErrs
errLen := len(mergedErrs)
strs := make([]string, errLen)
totalLen := 0
for i, err := range mergedErrs {
str := err.Error()
strs[i] = str
totalLen += len(str)
}
format := strings.Builder{}
format.Grow(totalLen + errLen*2)
format.WriteString(strs[0])
for _, str := range strs[1:] {
format.WriteString(", ")
format.WriteString(str)
}
str = format.String()
merged.str = str
return
}
func newError(str string) error {
return errors.New(str)
}
// errorF returns a formatted error
func errorF(format string, a ...interface{}) error {
return errors.New(fmt.Sprintf(format, a...))
}
func wrapper(nilIfFirstNil bool, err error, format string, a ...interface{}) error {
if err == nil {
if nilIfFirstNil {
return nil
}
return errorF(format, a...)
}
return &wrappedErr{
cause: err,
err: errorF(format, a...),
}
}
// wrapErrf wraps the given error, but only if it is not nil.
func wrapErrf(err error, format string, a ...interface{}) error {
return wrapper(true, err, format, a...)
}
// wrapToErrf is like wrapErrf but always returns an error
func wrapToErrf(err error, format string, a ...interface{}) error {
return wrapper(false, err, format, a...)
}
// mergeErrs merges an existing error with a new one
func mergeErrs(err error, format string, a ...interface{}) error {
newErr := errorF(format, a...)
if err == nil {
return newErr
}
var merged []error
if merge, isMergedErr := err.(*mergedErr); isMergedErr {
merged = append(append([]error(nil), merge.mergedErrs...), newErr)
} else {
merged = []error{err, newErr}
}
return &mergedErr{mergedErrs: merged}
}
// mergeErrors merges multiple errors
func mergeAllErrs(errs ...error) error {
var all []error
allLen := len(errs)
if allLen <= 1 {
if allLen == 0 {
return nil
}
return errs[0]
}
for _, err := range errs {
if err != nil {
if merge, isMergedErr := err.(*mergedErr); isMergedErr {
all = append(all, merge.mergedErrs...)
} else {
all = append(all, err)
}
}
}
allLen = len(all)
if allLen <= 1 {
if allLen == 0 {
return nil
}
return all[0]
}
return &mergedErr{mergedErrs: all}
}