-
Notifications
You must be signed in to change notification settings - Fork 0
/
TracedError.go
257 lines (197 loc) · 6.51 KB
/
TracedError.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
257
package asciichgolangpublic
import (
"errors"
"fmt"
"runtime"
"strings"
)
var ErrTracedError = errors.New("asciichgolangpublic TracedError base")
var ErrTracedErrorEmptyString = errors.New("asciichgolangpublic TracedError empty string")
var ErrTracedErrorNil = errors.New("asciichgolangpublic TracedError nil")
var ErrTracedErrorNotImplemented = errors.New("asciichgolangpublic TracedError not implemented")
type TracedErrorType struct {
formattedError error
functionCalls []string
errorsToUnwrap []error
}
// Create a new error with given error or error message.
// TracedErrors extends the error message by a human readable stack trace.
// Error wrapping using '%w' in format string is supported.
func TracedErrorf(formatString string, args ...interface{}) (tracedError error) {
return TracedError(
fmt.Errorf(formatString, args...),
)
}
// Create a new error with given error or error message.
// TracedErrors extends the error message by a human readable stack trace.
func TracedError(errorMessageOrError interface{}) (tracedError error) {
var internalError error = nil
switch errorToAdd := errorMessageOrError.(type) {
case error:
internalError = errorToAdd
default:
internalError = fmt.Errorf("%v", errorToAdd)
}
tracedErrorToReturn := TracedErrorType{
formattedError: internalError,
}
programCounters := make([]uintptr, 128)
const skipNewTracedError = 1
const skipRuntimeCallers = 1
const skipFrames = skipNewTracedError + skipRuntimeCallers
n_callers := runtime.Callers(skipFrames, programCounters)
frames := runtime.CallersFrames(programCounters[:n_callers])
more := true
for more {
var stackFrameInfo runtime.Frame
stackFrameInfo, more = frames.Next()
if stackFrameInfo.Function == "asciichgolangpublic.TracedErrorf" {
continue
}
if stackFrameInfo.Function == "asciichgolangpublic.TracedError" {
continue
}
if stackFrameInfo.Function == "testing.tRunner" {
break
}
if stackFrameInfo.Function == "runtime.main" {
break
}
stackInfoString := fmt.Sprintf("%s (%s:%d)", stackFrameInfo.Function, stackFrameInfo.File, stackFrameInfo.Line)
tracedErrorToReturn.functionCalls = append([]string{stackInfoString}, tracedErrorToReturn.functionCalls...)
}
return tracedErrorToReturn
}
func NewTracedErrorType() (t *TracedErrorType) {
return new(TracedErrorType)
}
func TracedErrorEmptyString(stringVarName string, errorToUnwrap ...error) (tracedError error) {
var toReturn TracedErrorType = TracedErrorf("'%s' is empty string", stringVarName).(TracedErrorType)
toReturn.errorsToUnwrap = append(toReturn.errorsToUnwrap, ErrTracedErrorEmptyString)
toReturn.errorsToUnwrap = append(toReturn.errorsToUnwrap, errorToUnwrap...)
return toReturn
}
func TracedErrorNil(nilVarName string) (tracedError error) {
var toReturn TracedErrorType = TracedErrorf("'%s' is nil", nilVarName).(TracedErrorType)
toReturn.errorsToUnwrap = append(toReturn.errorsToUnwrap, ErrTracedErrorNil)
return toReturn
}
func TracedErrorNilf(formatString string, args ...interface{}) (tracedError error) {
message := fmt.Sprintf(formatString, args...)
return TracedErrorNil(message)
}
func TracedErrorNotImplemented() (tracedError error) {
var toReturn TracedErrorType = TracedError("Not implemented").(TracedErrorType)
toReturn.errorsToUnwrap = append(toReturn.errorsToUnwrap, ErrTracedErrorNotImplemented)
return toReturn
}
func (t *TracedErrorType) GetErrorsToUnwrap() (errorsToUnwrap []error, err error) {
if t.errorsToUnwrap == nil {
return nil, TracedErrorf("errorsToUnwrap not set")
}
if len(t.errorsToUnwrap) <= 0 {
return nil, TracedErrorf("errorsToUnwrap has no elements")
}
return t.errorsToUnwrap, nil
}
func (t *TracedErrorType) GetFormattedError() (formattedError error, err error) {
return t.formattedError, nil
}
func (t *TracedErrorType) GetFunctionCalls() (functionCalls []string, err error) {
if t.functionCalls == nil {
return nil, TracedErrorf("functionCalls not set")
}
if len(t.functionCalls) <= 0 {
return nil, TracedErrorf("functionCalls has no elements")
}
return t.functionCalls, nil
}
func (t *TracedErrorType) MustGetErrorsToUnwrap() (errorsToUnwrap []error) {
errorsToUnwrap, err := t.GetErrorsToUnwrap()
if err != nil {
LogGoErrorFatal(err)
}
return errorsToUnwrap
}
func (t *TracedErrorType) MustGetFormattedError() (formattedError error) {
formattedError, err := t.GetFormattedError()
if err != nil {
LogGoErrorFatal(err)
}
return formattedError
}
func (t *TracedErrorType) MustGetFunctionCalls() (functionCalls []string) {
functionCalls, err := t.GetFunctionCalls()
if err != nil {
LogGoErrorFatal(err)
}
return functionCalls
}
func (t *TracedErrorType) MustSetErrorsToUnwrap(errorsToUnwrap []error) {
err := t.SetErrorsToUnwrap(errorsToUnwrap)
if err != nil {
LogGoErrorFatal(err)
}
}
func (t *TracedErrorType) MustSetFormattedError(formattedError error) {
err := t.SetFormattedError(formattedError)
if err != nil {
LogGoErrorFatal(err)
}
}
func (t *TracedErrorType) MustSetFunctionCalls(functionCalls []string) {
err := t.SetFunctionCalls(functionCalls)
if err != nil {
LogGoErrorFatal(err)
}
}
func (t *TracedErrorType) SetErrorsToUnwrap(errorsToUnwrap []error) (err error) {
if errorsToUnwrap == nil {
return TracedErrorf("errorsToUnwrap is nil")
}
if len(errorsToUnwrap) <= 0 {
return TracedErrorf("errorsToUnwrap has no elements")
}
t.errorsToUnwrap = errorsToUnwrap
return nil
}
func (t *TracedErrorType) SetFormattedError(formattedError error) (err error) {
t.formattedError = formattedError
return nil
}
func (t *TracedErrorType) SetFunctionCalls(functionCalls []string) (err error) {
if functionCalls == nil {
return TracedErrorf("functionCalls is nil")
}
if len(functionCalls) <= 0 {
return TracedErrorf("functionCalls has no elements")
}
t.functionCalls = functionCalls
return nil
}
func (t TracedErrorType) Error() (errorMessage string) {
errorMessage = ""
if len(t.functionCalls) > 0 {
errorMessage += strings.Join(t.functionCalls, "\n")
errorMessage += "\n"
errorMessage += "\n"
}
errorMessage += t.formattedError.Error()
return errorMessage
}
func (t TracedErrorType) Unwrap() (errors []error) {
errors = []error{
ErrTracedError, // Every traced error is automatically a ErrTraccedError.
}
errors = append(errors, t.formattedError)
if t.errorsToUnwrap != nil {
errors = append(errors, t.errorsToUnwrap...)
}
switch x := t.formattedError.(type) {
case interface{ Unwrap() error }:
errors = append(errors, x.Unwrap())
case interface{ Unwrap() []error }:
errors = append(errors, x.Unwrap()...)
}
return errors
}