-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
81 lines (77 loc) · 1.81 KB
/
Copy pathgenerate.go
File metadata and controls
81 lines (77 loc) · 1.81 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
70
71
72
73
74
75
76
77
78
79
80
81
package errorutils
import (
"errors"
"github.com/urfave/cli/v3"
)
// Adds context to an error. Subtle nil check: returns nil if both error and withInner are nil. Bubbles up inner if err is nil. WithMessage does *not* guarantee a new error report (use NewReport); undefined behavior with multiple withInner options.
func New(err error, o ...Option) *Details {
var wantsInner int = -1
for i, opt := range o {
if compareOptions(opt, WithInner(nil)) {
wantsInner = i
}
}
//must check if the inner error is nil, otherwise retain it
val, isDtl := err.(*Details)
if err == nil || (isDtl && val == nil) {
if wantsInner < 0 {
return nil
}
err = New(errors.New("wrapper"), o[wantsInner]).Unwrap()
if err == nil {
return nil
}
val, isDtl = err.(*Details) //necessary juggling to extract from closure
if val == nil {
return nil //avoid unnecessary typed error
}
}
e := &Details{}
//if error is of type Details apply options and return
if isDtl {
e = val
for _, opt := range o {
opt(e)
}
return e
}
if _, ok := err.(cli.ExitCoder); ok {
o = append(o, WithExitCode(err.(cli.ExitCoder).ExitCode()))
}
//test is one of the Options is withMsg
hasmsg := false
for _, opt := range o {
if compareOptions(opt, WithMsg("")) {
hasmsg = true
}
}
if hasmsg {
e = &Details{
inner: err,
exitcode: 1,
}
} else {
e = &Details{
msg: err.Error(),
exitcode: 1,
}
}
for _, opt := range o {
opt(e)
}
return e
}
// Succinct syntax for new error report
//
// Recommended for errors that could potentially be bugs, otherwise use fmt.Errorf. LineRef is only used in debug mode
func NewReport(msg string, lineRef string, o ...Option) *Details {
d := &Details{
msg: msg,
lineRef: lineRef,
exitcode: 1, //default exit code
}
for _, opt := range o {
opt(d)
}
return d
}