-
Notifications
You must be signed in to change notification settings - Fork 0
/
meep.go
80 lines (64 loc) · 1.33 KB
/
meep.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
package meep
func Meep(err error, opts ...Opts) error {
return New(err, opts...)
}
func New(err error, opts ...Opts) error {
opt := flattenOpts(opts)
// for `TraitAutodescribing`:
if m, ok := err.(meepAutodescriber); ok {
m.isMeepAutodescriber().self = err
}
// for `TraitTraceable`:
if m, ok := err.(meepTraceable); ok {
if !opt.nostack {
m.isMeepTraceable().Stack = *captureStack()
}
}
// for `TraitCausable`:
if m, ok := err.(meepCausable); ok {
if opt.cause != nil {
m.isMeepCausable().Cause = opt.cause
}
}
return err
}
type Opts struct {
cause error
nostack bool
}
func flattenOpts(opts []Opts) Opts {
v := Opts{}
for _, o := range opts {
if o.cause != nil {
v.cause = o.cause
}
if o.nostack == true {
v.nostack = true
}
}
return v
}
/*
Use `Cause` to tell `Meep()` that it should attach another error as a
cause to the error it's initializating.
Usage:
meep.Meep(
&ErrSomethingCausable{},
meep.Cause(fmt.Errorf("the root cause")),
)
*/
func Cause(x error) Opts {
return Opts{cause: New(x)}
}
/*
Use `NoStack` to tell `Meep()` that it should skip gathering a stack trace
for this error, even if it has `TraitTraceable`.
Usage:
meep.Meep(
&ErrUsuallyHasAStacktrace{},
meep.NoStack(), // skip stacks this time.
)
*/
func NoStack() Opts {
return Opts{nostack: true}
}