-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic.go
More file actions
69 lines (57 loc) · 1.7 KB
/
Copy pathpanic.go
File metadata and controls
69 lines (57 loc) · 1.7 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
// Package errgroup provides a more robust error group implementation
// that extends golang.org/x/sync/errgroup with panic recovery.
package errgroup
import (
"fmt"
"runtime/debug"
)
// A PanicError wraps an error recovered from an unhandled panic
// when calling a function passed to Go or TryGo.
type PanicError struct {
// Recovered is the error value that was passed to panic.
Recovered error
// Stack is the stack trace captured at the point of the panic.
Stack []byte
}
func (p PanicError) Error() string {
// A Go Error method conventionally does not include a stack dump, so omit it
// here. (Callers who care can extract it from the Stack field.)
return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered)
}
func (p PanicError) Unwrap() error { return p.Recovered }
// A PanicValue wraps a value that does not implement the error interface,
// recovered from an unhandled panic when calling a function passed to Go or
// TryGo.
type PanicValue struct {
// Recovered is the value that was passed to panic.
Recovered any
// Stack is the stack trace captured at the point of the panic.
Stack []byte
}
func (p PanicValue) String() string {
if len(p.Stack) > 0 {
return fmt.Sprintf("recovered from errgroup.Group: %v\n%s", p.Recovered, p.Stack)
}
return fmt.Sprintf("recovered from errgroup.Group: %v", p.Recovered)
}
func exception(recovered any) any {
if recovered == nil {
return nil
}
if p, ok := recovered.(PanicError); ok {
return p
}
if p, ok := recovered.(PanicValue); ok {
return p
}
if err, ok := recovered.(error); ok {
return PanicError{
Recovered: err,
Stack: debug.Stack(),
}
}
return PanicValue{
Recovered: recovered,
Stack: debug.Stack(),
}
}