-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapwithfuncparams.go
More file actions
214 lines (190 loc) · 7.41 KB
/
wrapwithfuncparams.go
File metadata and controls
214 lines (190 loc) · 7.41 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
package errs
/*
Call argument parameters are available on the stack,
but in a platform dependent packed format and not directly accessible
via package runtime.
Could only be parsed from runtime.Stack() result text.
See https://www.ardanlabs.com/blog/2015/01/stack-traces-in-go.html
*/
func wrapWithFuncParamsSkip(skip int, err error, params ...any) *withCallStackFuncParams {
switch w := err.(type) {
case callStackParamsProvider:
// OK, wrap the wrapped
case callStackProvider:
// Already wrapped with call stack,
// replace with withCallStackFuncParams
return &withCallStackFuncParams{
withCallStack: withCallStack{
err: w.Unwrap(),
callStack: w.CallStack(),
},
params: params,
}
}
return &withCallStackFuncParams{
withCallStack: withCallStack{
err: err,
callStack: callStack(skip + 1),
},
params: params,
}
}
// WrapWithFuncParamsSkip wraps an error with the current call stack and function parameters,
// skipping skip stack frames.
//
// The skip parameter specifies how many stack frames to skip
// before capturing the call stack. Use skip=0 to capture the stack
// from the immediate caller of WrapWithFuncParamsSkip.
// Increase skip by 1 for each additional function wrapper you add.
//
// This function is typically used in defer statements to automatically
// wrap errors with the function's parameters when the function returns an error.
//
// Examples:
//
// // Standard usage in defer - skip=0 is correct
// func ProcessUser(userID string, age int) (err error) {
// defer WrapWithFuncParamsSkip(0, &err, userID, age)
// // ... function body
// return someError
// }
//
// // Wrapper function - skip=1+skip to pass through skip count
// func myWrapperSkip(skip int, resultVar *error, params ...any) {
// WrapWithFuncParamsSkip(1+skip, resultVar, params...)
// }
//
// // Most common: use WrapWithFuncParams which has skip=0 built-in
// func ProcessUser(userID string, age int) (err error) {
// defer WrapWithFuncParams(&err, userID, age)
// // ... function body
// return someError
// }
func WrapWithFuncParamsSkip(skip int, resultVar *error, params ...any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1+skip, *resultVar, params...)
}
}
// WrapWithFuncParams wraps an error with the current call stack and function parameters.
//
// This is the most commonly used function for wrapping errors with function parameters
// in defer statements. It automatically captures the correct call stack frame.
//
// Example:
//
// func ProcessUser(userID string, age int) (err error) {
// defer WrapWithFuncParams(&err, userID, age)
//
// if age < 0 {
// return errors.New("invalid age")
// }
// // When an error is returned, it will be wrapped with:
// // - The call stack showing ProcessUser(userID, age)
// // - The actual parameter values passed to the function
// return someOperation(userID)
// }
//
// Note: For functions with 0-10 parameters, use the optimized variants
// WrapWith0FuncParams through WrapWith10FuncParams for better performance.
func WrapWithFuncParams(resultVar *error, params ...any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, params...)
}
}
// WrapWith0FuncParams wraps an error with the call stack for functions with no parameters.
// This is more efficient than WrapWithFuncParams for zero-parameter functions.
func WrapWith0FuncParams(resultVar *error) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar)
}
}
// WrapWith1FuncParam wraps an error with the call stack and 1 function parameter.
// This is more efficient than WrapWithFuncParams for single-parameter functions.
func WrapWith1FuncParam(resultVar *error, p0 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0)
}
}
// WrapWith2FuncParams wraps an error with the call stack and 2 function parameters.
// This is more efficient than WrapWithFuncParams for two-parameter functions.
func WrapWith2FuncParams(resultVar *error, p0, p1 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1)
}
}
// WrapWith3FuncParams wraps an error with the call stack and 3 function parameters.
// This is more efficient than WrapWithFuncParams for three-parameter functions.
func WrapWith3FuncParams(resultVar *error, p0, p1, p2 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2)
}
}
// WrapWith4FuncParams wraps an error with the call stack and 4 function parameters.
// This is more efficient than WrapWithFuncParams for four-parameter functions.
func WrapWith4FuncParams(resultVar *error, p0, p1, p2, p3 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3)
}
}
// WrapWith5FuncParams wraps an error with the call stack and 5 function parameters.
// This is more efficient than WrapWithFuncParams for five-parameter functions.
func WrapWith5FuncParams(resultVar *error, p0, p1, p2, p3, p4 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4)
}
}
// WrapWith6FuncParams wraps an error with the call stack and 6 function parameters.
// This is more efficient than WrapWithFuncParams for six-parameter functions.
func WrapWith6FuncParams(resultVar *error, p0, p1, p2, p3, p4, p5 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4, p5)
}
}
// WrapWith7FuncParams wraps an error with the call stack and 7 function parameters.
// This is more efficient than WrapWithFuncParams for seven-parameter functions.
func WrapWith7FuncParams(resultVar *error, p0, p1, p2, p3, p4, p5, p6 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4, p5, p6)
}
}
// WrapWith8FuncParams wraps an error with the call stack and 8 function parameters.
// This is more efficient than WrapWithFuncParams for eight-parameter functions.
func WrapWith8FuncParams(resultVar *error, p0, p1, p2, p3, p4, p5, p6, p7 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4, p5, p6, p7)
}
}
// WrapWith9FuncParams wraps an error with the call stack and 9 function parameters.
// This is more efficient than WrapWithFuncParams for nine-parameter functions.
func WrapWith9FuncParams(resultVar *error, p0, p1, p2, p3, p4, p5, p6, p7, p8 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4, p5, p6, p7, p8)
}
}
// WrapWith10FuncParams wraps an error with the call stack and 10 function parameters.
// This is more efficient than WrapWithFuncParams for ten-parameter functions.
func WrapWith10FuncParams(resultVar *error, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9 any) {
if *resultVar != nil {
*resultVar = wrapWithFuncParamsSkip(1, *resultVar, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)
}
}
type callStackParamsProvider interface {
callStackProvider
CallStackParams() ([]uintptr, []any)
}
var (
_ error = &withCallStackFuncParams{}
_ callStackProvider = &withCallStackFuncParams{}
_ callStackParamsProvider = &withCallStackFuncParams{}
)
// withCallStackFuncParams is an error wrapper that implements callStackParamsProvider
type withCallStackFuncParams struct {
withCallStack
params []any
}
func (w *withCallStackFuncParams) Error() string {
return formatError(w)
}
func (w *withCallStackFuncParams) CallStackParams() ([]uintptr, []any) {
return w.callStack, w.params
}