-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
72 lines (64 loc) · 1.27 KB
/
error.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 utils
import (
"context"
"errors"
"fmt"
"k8s.io/klog/v2"
)
func LogError(err error) {
klog.ErrorDepth(1, err)
}
func LogAndReturnError(err error) error {
klog.ErrorDepth(1, err)
return err
}
func LogAndReturnErrorWithCtx(ctx context.Context, err error) error {
ErrorWithCtx(ctx, err.Error(), 1)
return err
}
func RecoverAndLogError() {
if err := recover(); err != nil {
klog.ErrorDepth(3, err)
}
}
func RecoverAndLogErrorWithCtx(ctx context.Context) {
if err := recover(); err != nil {
switch e := err.(type) {
case error:
ErrorWithCtx(ctx, e.Error(), 3)
case string:
ErrorWithCtx(ctx, e, 3)
default:
ErrorWithCtx(ctx, fmt.Sprintf("%+v", e), 3)
}
}
}
func RecoverAndLogAndWriteError(errVar *error) {
err := recover()
if err != nil {
klog.ErrorDepth(3, err)
switch e := err.(type) {
case string:
*errVar = errors.New(e)
case error:
*errVar = e
}
}
}
func RecoverAndLogAndWriteErrorWithCtx(ctx context.Context, errVar *error) {
err := recover()
if err != nil {
switch e := err.(type) {
case error:
ErrorWithCtx(ctx, e.Error(), 3)
*errVar = e
case string:
ErrorWithCtx(ctx, e, 3)
*errVar = errors.New(e)
default:
msg := fmt.Sprintf("%+v", e)
ErrorWithCtx(ctx, msg, 3)
*errVar = errors.New(msg)
}
}
}