-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctxerrors.go
More file actions
165 lines (133 loc) · 3.7 KB
/
Copy pathctxerrors.go
File metadata and controls
165 lines (133 loc) · 3.7 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
package ctxerrors
import (
"errors"
"fmt"
"log/slog"
"runtime"
)
// CTXError holds the wrapped error and additional context.
type CTXError struct {
err error
message string
file string
line int
funcName string
}
// New creates a new error with context but without wrapping another error.
func New(message string) error {
// Skip New() to get user's caller
framesToSkip := 1
file, line, funcName := getCallerInfo(framesToSkip)
return &CTXError{
message: message,
file: file,
line: line,
funcName: funcName,
}
}
// Wrap wraps an error with context information (file, line, and function name).
func Wrap(err error, message string) error {
// Skip Wrap() and wrap() to get user's caller
framesToSkip := 2
return wrap(err, message, framesToSkip)
}
// Wrapf wraps an error with context information (file, line, and function name).
func Wrapf(err error, format string, args ...any) error {
// Skip Wrapf() and wrap() to get user's caller
framesToSkip := 2
return wrap(err, fmt.Sprintf(format, args...), framesToSkip)
}
// Join combines several errors into one that carries the call site, for the
// case where an operation fans out and more than one branch can fail
// independently — several sinks written to, several items in a batch. nil
// errors are ignored, and Join returns nil when every error is nil.
//
// The result unwraps to what the standard library's errors.Join produces, so
// errors.Is and errors.As still find any of the joined errors.
func Join(errs ...error) error {
joined := errors.Join(errs...)
if joined == nil {
return nil
}
count := 0
for _, err := range errs {
if err != nil {
count++
}
}
// Skip Join() to get user's caller
framesToSkip := 1
file, line, funcName := getCallerInfo(framesToSkip)
return &CTXError{
err: joined,
message: fmt.Sprintf("%d errors", count),
file: file,
line: line,
funcName: funcName,
}
}
func wrap(err error, message string, skip int) error {
if err == nil {
// log three frames so callers can see the actual call chain that
// produced the nil, not just the immediate caller
debugFrame1 := 2
debugFrame2 := 3
debugFrame3 := 4
pc, file, line, _ := runtime.Caller(debugFrame1)
funcName := runtime.FuncForPC(pc).Name()
pc2, file2, line2, _ := runtime.Caller(debugFrame2)
funcName2 := runtime.FuncForPC(pc2).Name()
pc3, file3, line3, _ := runtime.Caller(debugFrame3)
funcName3 := runtime.FuncForPC(pc3).Name()
slog.Error("Trying to wrap a nil error",
"sourceFile1", fmt.Sprintf("%s:%d", file, line),
"sourceFunc1", funcName,
"sourceFile2", fmt.Sprintf("%s:%d", file2, line2),
"sourceFunc2", funcName2,
"sourceFile3", fmt.Sprintf("%s:%d", file3, line3),
"sourceFunc3", funcName3,
)
return nil
}
err = translate(err)
file, line, funcName := getCallerInfo(skip)
return &CTXError{
err: err,
message: message,
file: file,
line: line,
funcName: funcName,
}
}
// Unwrap retrieves the underlying error, if any.
func (e *CTXError) Unwrap() error {
if e == nil {
return nil
}
return e.err
}
// Error returns the formatted error message, including file and function details.
func (e *CTXError) Error() string {
if e == nil {
return ""
}
if e.err != nil {
return fmt.Sprintf(
"%s: %s [%s:%d in %s]",
e.message, e.err, e.file, e.line, e.funcName,
)
}
return fmt.Sprintf(
"%s [%s:%d in %s]",
e.message, e.file, e.line, e.funcName,
)
}
// getCallerInfo retrieves file, line, and function name where the error was created.
func getCallerInfo(skip int) (string, int, string) {
pc, file, line, ok := runtime.Caller(skip + 1)
if !ok {
return "", 0, ""
}
funcName := runtime.FuncForPC(pc).Name()
return file, line, funcName
}