-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrace.go
More file actions
167 lines (153 loc) · 3.49 KB
/
trace.go
File metadata and controls
167 lines (153 loc) · 3.49 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
package server
import (
// "context"
// "sync"
"time"
// "slices"
// "os"
// "os/signal"
// "syscall"
"encoding/json"
"fmt"
"runtime/debug"
"strings"
// "reflect"
// mathrand "math/rand"
"github.com/urnetwork/glog"
)
func IsDoneError(r any) bool {
isDoneMessage := func(message string) bool {
switch {
case strings.HasPrefix(message, "Done"):
return true
// pgx
case strings.Contains(message, "context canceled"),
strings.HasPrefix(message, "failed to deallocate cached statement(s)"):
return true
default:
return false
}
}
switch v := r.(type) {
case error:
return isDoneMessage(v.Error())
case string:
return isDoneMessage(v)
default:
return false
}
}
// this is meant to handle unexpected errors and do some cleanup
func HandleError(do func(), handlers ...any) (r any) {
defer func() {
if r = recover(); r != nil {
if IsDoneError(r) {
// the context was canceled and raised. this is a standard pattern, do not log
} else {
glog.Infof("Unexpected error: %s\n", ErrorJson(r, debug.Stack()))
}
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%s", r)
}
for _, handler := range handlers {
switch v := handler.(type) {
case func():
v()
case func(error):
v(err)
}
}
}
}()
do()
return
}
func HandleError1[R any](do func() R, handlers ...any) (result R) {
HandleError(func() {
result = do()
}, func(err error) {
for _, handler := range handlers {
switch v := handler.(type) {
case func() R:
result = v()
case func(error) R:
result = v(err)
}
}
})
return
}
func HandleError2[R1 any, R2 any](do func() (R1, R2), handlers ...any) (result1 R1, result2 R2) {
HandleError(func() {
result1, result2 = do()
}, func(err error) {
for _, handler := range handlers {
switch v := handler.(type) {
case func() (R1, R2):
result1, result2 = v()
case func(error) (R1, R2):
result1, result2 = v(err)
}
}
})
return
}
func HandleErrorWithReturn[R any](do func() R) (result R) {
return HandleError1(do)
}
func ErrorJson(err any, stack []byte) string {
stackLines := []string{}
for _, line := range strings.Split(string(stack), "\n") {
stackLines = append(stackLines, strings.TrimSpace(line))
}
errorJson, _ := json.Marshal(map[string]any{
"error": fmt.Sprintf("%T=%s", err, err),
"stack": stackLines,
})
return string(errorJson)
}
func ErrorJsonNoStack(err any) string {
errorJson, _ := json.Marshal(map[string]any{
"error": fmt.Sprintf("%T=%s", err, err),
})
return string(errorJson)
}
func ErrorJsonWithCustomNoStack(err any, custom map[string]any) string {
obj := map[string]any{
"error": fmt.Sprintf("%T=%s", err, err),
}
for key, value := range custom {
obj[key] = value
}
errorJson, _ := json.Marshal(obj)
return string(errorJson)
}
func Trace(tag string, do func()) {
trace(tag, func() string {
do()
return ""
})
}
func TraceWithReturn[R any](tag string, do func() R) (result R) {
trace(tag, func() string {
result = do()
return fmt.Sprintf(" = %v", result)
})
return
}
func TraceWithReturnShallowLog[R any](tag string, do func() R) (result R) {
trace(tag, func() string {
result = do()
return fmt.Sprintf(" = %T", result)
})
return
}
func trace(tag string, do func() string) {
start := time.Now()
glog.Infof("[%-8s]%s (%d)\n", "start", tag, start.UnixMilli())
doTag := do()
end := time.Now()
millis := float32(end.Sub(start)) / float32(time.Millisecond)
glog.Infof("[%-8s]%s (%.2fms) (%d)%s\n", "end", tag, millis, end.UnixMilli(), doTag)
}