generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
66 lines (56 loc) · 1.11 KB
/
helpers.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
package plex
import (
"context"
"fmt"
)
// _ctx returns a valid context with cancel even if it the
// supplied context is initially nil. If the supplied context
// is nil it uses the background context
func _ctx(c context.Context) (context.Context, context.CancelFunc) {
if c == nil {
c = context.Background()
}
return context.WithCancel(c)
}
func merge(parent, child context.Context) context.Context {
if child == nil {
return parent
}
return child
}
type errWrapper struct {
err string
underlying error
}
func (e *errWrapper) Error() string {
return e.err
}
func (e *errWrapper) Unwrap() error {
return e.underlying
}
func recoverErr(err error, r interface{}) error {
switch v := r.(type) {
case nil:
if err != nil {
return err
}
return nil
case string:
return &errWrapper{
err: v,
underlying: err,
}
case error:
return &errWrapper{
err: v.Error(),
underlying: err,
}
default:
// Fallback err (per specs, error strings
// should be lowercase w/o punctuation
return &errWrapper{
err: fmt.Sprintf("panic: %v", r),
underlying: err,
}
}
}