-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.go
66 lines (57 loc) · 1.58 KB
/
panic.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
// Copyright (c) 2020, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
"fmt"
)
// WrapPanic wraps a panicking sequence with the given prefix.
// It then panics again.
func WrapPanic(prefix string) {
if r := recover(); r != nil {
panic(fmt.Sprintf("%s: %s", prefix, r))
}
}
// Must panics when any of the given args is a non-nil error.
// Its message is the error message of the first encountered error.
func Must(args ...interface{}) {
for _, arg := range args {
if err, ok := arg.(error); ok && err != nil {
panic(fmt.Sprintf("errors.Must: %+v", err))
}
}
}
// CatchPanic recovers from a panic and wraps it in an error. It then calls
// AppendInto with the provided dest *error and wrapped panic.
// Use CatchPanic directly with defer. It is not possible to use CatchPanic
// inside a deferred function, like:
//
// defer func(){ CatchPanic(&err }()
func CatchPanic(dest *error) {
if r := recover(); r != nil {
AppendInto(dest, newCommonErr(&panicError{v: r}, false, 1))
if st := GetStackTrace(*dest); st != nil {
st.Skip = 1
}
}
}
type panicError struct{ v interface{} }
func (p *panicError) Unwrap() error {
if e, ok := p.v.(error); ok {
return e
}
return nil
}
func (p *panicError) Error() string {
switch v := p.v.(type) {
case error:
return fmt.Sprintf("panic: %+v", p.v)
case string:
return "panic: " + v
default:
return fmt.Sprintf("panic: %v", p.v)
}
}
func (p *panicError) GoString() string {
return fmt.Sprintf("errors.panicError{v: %#v}", p.v)
}