-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrors.go
191 lines (157 loc) · 3.47 KB
/
rrors.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
package rrors
import (
"errors"
"fmt"
"path"
"path/filepath"
"runtime"
"strings"
)
type rror struct {
wrap error
tmpl string
}
func (r rror) Error() string {
v, _ := formatMultipleErr(r)
return v
}
func (r rror) Unwrap() error {
return r.wrap
}
type rrors struct {
msg string
wraps []error
}
func (r rrors) Unwrap() []error {
return r.wraps
}
func (r rrors) Error() string {
v, _ := formatMultipleErr(r)
return v
}
func formatMultipleErr(mainErr error) (v string, replace string) {
if ror, ok := mainErr.(rror); ok {
v, _ := formatMultipleErr(ror.wrap)
return ConnectString(ror.tmpl, []string{v}), ""
}
rors, ok := mainErr.(rrors)
if !ok {
err := mainErr
if multiple, ok := err.(interface{ Unwrap() []error }); ok {
v, _ := formatMultipleErr(rrors{msg: ">", wraps: multiple.Unwrap()})
return v, err.Error()
}
unwrap := errors.Unwrap(err)
if unwrap == nil {
return err.Error(), err.Error()
}
v, replace := formatMultipleErr(unwrap)
return ConnectString(strings.Replace(err.Error(), replace, "", 1), []string{v}), err.Error()
}
strs := []string{}
for _, err := range rors.wraps {
v, _ := formatMultipleErr(err)
strs = append(strs, v)
}
return ConnectString(rors.msg, strs), ""
}
func iterateErrsWithArgs(tmpl string, args []any, cb func(arg any)) (string, []any) {
i := 0
currArg := 0
skipIndexes := []int{}
nonErrArgs := []any{}
for i < len(tmpl) {
for i < len(tmpl) && tmpl[i] != '%' {
i++
}
if i >= len(tmpl) {
continue
}
// found %
j := i + 1
if j < len(tmpl) && tmpl[j] != '%' && tmpl[j] != ' ' {
j++
}
if j < len(tmpl) && tmpl[j] == '%' {
for j < len(tmpl) && tmpl[j] != '%' {
j++
}
}
if tmpl[i:j] == "%w" {
cb(args[currArg])
currArg++
skipIndexes = append(skipIndexes, i)
} else {
nonErrArgs = append(nonErrArgs, args[currArg])
currArg++
}
i = j
}
builder := strings.Builder{}
prev := 0
for _, idx := range skipIndexes {
builder.WriteString(tmpl[prev:idx])
prev = idx + 2
}
builder.WriteString(tmpl[prev:])
return builder.String(), nonErrArgs
}
var basePath = ""
func split(p string) []string {
s := []string{}
for p != "/" && len(p) != 0 {
path, rest := filepath.Split(p)
s = append([]string{rest}, s...)
p = path[:len(path)-1]
}
return s
}
func common(target, path2 string) string {
pathSplit, pathSplit2 := split(target), split(path2)
for i := range pathSplit {
if i != len(pathSplit)-1 && i < len(pathSplit2) && pathSplit[i] == pathSplit2[i] {
continue
}
return path.Join(pathSplit[i:]...)
}
return path.Join(pathSplit[len(pathSplit)-1:]...)
}
func Initialise() {
_, file, _, _ := runtime.Caller(1)
basePath = file
}
func Errorf(tmpl string, args ...any) error {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "?"
line = 0
}
file = common(file, basePath)
lineStr := "at " + file + fmt.Sprintf(":%d", line)
idx := strings.Index(tmpl, "%w")
if idx < 0 {
return fmt.Errorf(tmpl+" "+lineStr, args...)
}
var errs []error
newTmpl, argsNoErr := iterateErrsWithArgs(tmpl, args, func(arg any) {
if err, ok := arg.(error); ok {
errs = append(errs, err)
} else {
panic("so... how do we handle this?")
}
})
if len(errs) == 1 {
er := errs[0]
if mul, ok := er.(interface{ Unwrap() []error }); ok {
return rrors{
wraps: mul.Unwrap(),
msg: newTmpl + lineStr,
}
}
return rror{er, fmt.Sprintf(newTmpl+lineStr, argsNoErr...)}
}
return rrors{
wraps: errs,
msg: fmt.Sprintf(newTmpl, argsNoErr...),
}
}