-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
42 lines (37 loc) · 875 Bytes
/
functions.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
package cerror
import (
"fmt"
"runtime"
"strings"
)
func getStackTraces() string {
var traces strings.Builder
rpc := make([]uintptr, 10)
n := runtime.Callers(5, rpc[:])
if n <= 0 {
return traces.String()
}
frames := runtime.CallersFrames(rpc)
for i := 0; i < n; i++ {
frame, _ := frames.Next()
if frame.Func != nil && !strings.Contains(frame.File, "go/src/runtime") {
traces.WriteString(fmt.Sprintf("[%s] %s:%d\n", frame.Function, frame.File, frame.Line))
}
}
return traces.String()
}
func getErrorCode() string {
rpc := make([]uintptr, 10)
n := runtime.Callers(3, rpc[:])
if n <= 0 {
return ""
}
frames := runtime.CallersFrames(rpc)
frame, _ := frames.Next()
if frame.Func != nil && strings.Contains(frame.Function, "Error") {
list := strings.Split(frame.Function, ".")
return list[len(list)-1]
} else {
return "CustomError"
}
}