-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.go
More file actions
309 lines (275 loc) · 7.25 KB
/
error.go
File metadata and controls
309 lines (275 loc) · 7.25 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package errcode
import (
"bytes"
"errors"
"fmt"
"hash/fnv"
"strings"
"sync/atomic"
)
type (
// ErrSeed is a builder for GeneralError types
ErrSeed interface {
SubType(errSubType string) ErrSeed
ExternalErrMess(err error) ErrSeed
Message(message string) ErrSeed
MessageF(format string, a ...interface{}) ErrSeed
Make() GeneralError
}
// GeneralError is custom error type builder designed for convenient error handling using
// native errors 1.13 implementation
GeneralError interface {
Error() string
FmtResponse(template string) string
ErrorCode() string
CodeNote() string
SysMessage() string
Unwrap() error
Is(err error) bool
Produce() ErrSeed
}
// generalError is GeneralError implementation
generalError struct {
codeGen func(string) string
sumCodes func(old, new string) string
err error
cause error
export atomic.Value
code string
codeNote string
mes string
}
)
// NewGeneralError is GeneralError constructor with default codes generators
func NewGeneralError(code string) ErrSeed {
return NewGeneralErrorWithCustomCodes(code, errCodeGen, sumCodes)
}
// NewGeneralErrorWithCustomCodes is GeneralError constructor with custom code generation
func NewGeneralErrorWithCustomCodes(code string, codeGen func(string string) string, inheritCodeGen func(old, new string) string) ErrSeed {
c := codeGen(code)
n := code
e := errors.New(c)
r := &generalError{
codeGen: codeGen,
sumCodes: inheritCodeGen,
err: e,
cause: e,
export: atomic.Value{},
code: c,
codeNote: concatenate(c, ":", n),
}
r.export.Store("")
return r
}
// ToGeneralError try to convert err to GeneralError
func ToGeneralError(err error) (GeneralError, bool) { e, ok := err.(GeneralError); return e, ok }
// IsGeneralError checks whether err is GeneralError type
func IsGeneralError(err error) bool { _, ok := ToGeneralError(err); return ok }
// Error returns string for error
func (r generalError) Error() string {
export := r.export.Load()
emptyExp := func() bool {
switch {
case export == nil:
return true
case strings.Contains(export.(string), r.SysMessage()):
return false
case strings.Contains(export.(string), r.ErrorCode()):
return false
default:
return true
}
}
if emptyExp() {
export = fmt.Sprintf("[%v: %v] %v", r.ErrorCode(), r.err, r.SysMessage())
// concatenate("[", r.ErrorCode(), ":", r.err.Error(), "] ", r.SysMessage())
r.export.Store(export)
}
return r.export.Load().(string)
}
// ExternalErrMess is adding message from error to GeneralError if it's not GeneralError entity
func (r *generalError) ExternalErrMess(err error) ErrSeed {
if err == nil {
return r
}
e, ok := ToGeneralError(err)
if ok && e.(*generalError).mes != "" {
return r
}
mes := func() string {
if r.mes == "" {
return err.Error()
}
return r.mes
}
res := r.clone()
res.mes = mes()
res.err = fmt.Errorf("%w: %v", r.err, err.Error())
res.cause = err
return res
}
// SubType creates a subtype of GeneralError
func (r generalError) SubType(errSubType string) ErrSeed {
c := r.codeGen(errSubType)
res := r.clone()
res.err = fmt.Errorf("%w.%v", r.err, c)
res.code = r.sumCodes(r.code, c)
res.cause = fmt.Errorf("%w.%v", r.err, c)
res.codeNote = fmt.Sprintf("%v ~> %v:%v", r.codeNote, c, errSubType)
return res
}
// Message describes error
func (r generalError) Message(message string) ErrSeed {
res := r.clone()
r.mes = message
return res
}
// MessageF describes error with formatting
func (r generalError) MessageF(format string, a ...interface{}) ErrSeed {
res := r.clone()
res.mes = fmt.Sprintf(format, a...)
return res
}
// Unwrap is used for errors.Is(...) and returns inner error
func (r generalError) Unwrap() error {
return r.err
}
// Cause is deprecated. Deprecated: used for backwards compatibility with pkg/errors
func (r generalError) Cause() error {
return r.cause
}
// Is checks whether this instance is descendant of provided error
func (r generalError) Is(err error) bool {
e, ok := ToGeneralError(err)
checkErr := err
if ok {
checkErr = e.Unwrap()
}
is := errors.Is(r.err, checkErr)
if !is {
// for non GeneralError compatibility
is = errors.Is(r.cause, checkErr)
if !is {
// here we should not use checkErr, and direct err because
// if it was unwrapped early due to GeneralError type,
// then we will get cause of unwrapped error, and not cause of this one
hasCause, cok := err.(interface{ Cause() error })
if cok {
errors.Is(r.cause, hasCause.Cause())
}
}
}
return is
}
// ErrorCode returns generated error code
func (r *generalError) ErrorCode() string {
return r.code
}
// CodeNote returns chain of notes for error codes
func (r *generalError) CodeNote() string {
return r.codeNote
}
// SysMessage returns message for this error
func (r generalError) SysMessage() string {
if r.mes == "" {
return r.codeNote
}
return r.mes
}
// FmtResponse formats response according to template with code of this error
func (r generalError) FmtResponse(template string) string {
return concatenate(template, "\nCODE: ", r.ErrorCode())
}
// Make produces instance of GeneralError
func (r *generalError) Make() GeneralError {
return r
}
// Produce is for producing subtype of GeneralError
func (r *generalError) Produce() ErrSeed {
return r
}
func (r *generalError) clone() *generalError {
return &generalError{
codeGen: r.codeGen,
sumCodes: r.sumCodes,
err: r.err,
cause: r.cause,
export: atomic.Value{},
code: r.code,
codeNote: r.codeNote,
mes: r.mes,
}
}
func errCodeGen(base string) string {
h := fnv.New32a()
_, err := h.Write([]byte(base))
if err != nil {
return ""
}
v := h.Sum32()
return uint32toStr36(v)
}
func sumCodes(code1, code2 string) string {
var base36map = map[rune]uint32{
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
'6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11,
'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17,
'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23,
'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 18, 'T': 29,
'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35,
}
var (
base36 = [...]string{
"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B",
"C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
}
)
touint32 := func(s string) uint32 {
radix := uint32(len(base36))
var res uint32
var weight uint32 = 1
for _, k := range s {
res += weight * base36map[k]
weight *= radix
}
return res
}
return uint32toStr36(touint32(code1) + touint32(code2))
}
func uint32toStr36(value uint32) string {
var (
base36 = [...]string{
"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B",
"C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
}
)
radix := uint32(len(base36))
var buffer bytes.Buffer
for i := value; i > 0; i /= radix {
k := i % radix
_, err := buffer.WriteString(base36[k])
if err != nil {
return ""
}
}
r := []rune(buffer.String())
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
func concatenate(strs ...string) string {
b := strings.Builder{}
for _, s := range strs {
b.WriteString(s)
}
return b.String()
}