-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanics.go
112 lines (95 loc) · 2.74 KB
/
panics.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
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
package errs
import (
"errors"
"fmt"
"runtime/debug"
"github.com/domonda/go-pretty"
)
// AsError converts any type to an error without wrapping it.
// Nil values will be converted to a nil error.
func AsError(val any) error {
switch x := val.(type) {
case nil:
return nil
case error:
return x
case []error:
return errors.Join(x...)
case string:
return errors.New(x)
case fmt.Stringer:
return errors.New(x.String())
default:
return errors.New(pretty.Sprint(val))
}
}
// AsErrorWithDebugStack converts any type to an error
// and if not nil wraps it with debug.Stack() after a newline.
// Nil values will be converted to a nil error.
func AsErrorWithDebugStack(val any) error {
err := AsError(val)
if err == nil {
return nil
}
return fmt.Errorf("%w\n%s", err, debug.Stack())
}
// LogPanicWithFuncParams recovers any panic,
// converts it to an error wrapped with the callstack
// of the panic and the passed function parameter values
// and prints it with the prefix "LogPanicWithFuncParams: "
// to the passed Logger.
// After logging, the original panic is re-paniced.
func LogPanicWithFuncParams(log Logger, params ...any) {
p := recover()
if p == nil {
return
}
err := AsErrorWithDebugStack(p)
err = wrapWithFuncParamsSkip(1, err, params...)
log.Printf("LogPanicWithFuncParams: %s", err.Error())
panic(p)
}
// RecoverAndLogPanicWithFuncParams recovers any panic,
// converts it to an error wrapped with the callstack
// of the panic and the passed function parameter values
// and prints it with the prefix "RecoverAndLogPanicWithFuncParams: "
// to the passed Logger.
func RecoverAndLogPanicWithFuncParams(log Logger, params ...any) {
p := recover()
if p == nil {
return
}
err := AsErrorWithDebugStack(p)
err = wrapWithFuncParamsSkip(1, err, params...)
log.Printf("RecoverAndLogPanicWithFuncParams: %s", err.Error())
}
// RecoverPanicAsError recovers any panic,
// converts it to an error wrapped with the callstack
// of the panic and assigns it to the result error.
func RecoverPanicAsError(result *error) {
p := recover()
if p == nil {
return
}
err := AsErrorWithDebugStack(p)
if *result != nil {
err = fmt.Errorf("function returning error (%s) paniced with: %w", *result, err)
}
*result = err
}
// RecoverPanicAsErrorWithFuncParams recovers any panic,
// converts it to an error wrapped with the callstack
// of the panic and the passed function parameter values
// and assigns it to the result error.
func RecoverPanicAsErrorWithFuncParams(result *error, params ...any) {
p := recover()
if p == nil {
return
}
err := AsErrorWithDebugStack(p)
err = wrapWithFuncParamsSkip(1, err, params...)
if *result != nil {
err = fmt.Errorf("function returning error (%s) paniced with: %w", *result, err)
}
*result = err
}