-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.go
72 lines (63 loc) · 1.37 KB
/
exception.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
package ego
import (
"fmt"
"path"
"runtime"
)
type Exception interface {
error
Callers() []uintptr
}
func Errorf(fmtStr string, a ...any) Exception {
var callers [32]uintptr
frames := runtime.Callers(2, callers[:])
return &exception{
error: fmt.Errorf(fmtStr, a...),
callers: callers[:frames],
}
}
type exception struct {
error
callers []uintptr
}
func addStack(skip int, err error) Exception {
if exception, ok := err.(Exception); ok {
return exception
}
var callers [32]uintptr
frames := runtime.Callers(2+skip, callers[:])
return &exception{
error: err,
callers: callers[:frames],
}
}
func (e *exception) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
// Print the error message and the stack trace
fmt.Fprintf(f, "%v\n", e.error)
frames := runtime.CallersFrames(e.callers)
for index := 0; ; index++ {
frame, more := frames.Next()
if more {
fmt.Fprintf(f, "%d: %s %s:%d\n", index, path.Base(frame.Function), path.Base(frame.File), frame.Line)
} else {
fmt.Fprintf(f, "%d: %s %s:%d", index, path.Base(frame.Function), path.Base(frame.File), frame.Line)
return
}
}
}
fallthrough
case 's':
fmt.Fprintf(f, "%s", e.Error())
case 'q':
fmt.Fprintf(f, "%q", e.Error())
}
}
func (e *exception) Unwrap() error {
return e.error
}
func (e *exception) Callers() []uintptr {
return e.callers
}