-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
error.go
83 lines (70 loc) · 1.93 KB
/
error.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
81
82
83
package wmenu
import "errors"
var (
//ErrInvalid is returned if a response from user was an invalid option
ErrInvalid = errors.New("invalid response")
//ErrTooMany is returned if multiSelect is false and a user tries to select multiple options
ErrTooMany = errors.New("too many responses")
//ErrNoResponse is returned if there were no responses and no action to call
ErrNoResponse = errors.New("no response")
//ErrDuplicate is returned is a user selects an option twice
ErrDuplicate = errors.New("duplicated response")
)
// MenuError records menu errors
type MenuError struct {
Err error
Res string
TriesLeft int
}
// Error prints the error in an easy to read string.
func (e *MenuError) Error() string {
if e.Res != "" {
return e.Err.Error() + ": " + e.Res
}
return e.Err.Error()
}
func newMenuError(err error, res string, tries int) *MenuError {
return &MenuError{
Err: err,
Res: res,
TriesLeft: tries,
}
}
// IsInvalidErr checks to see if err is of type invalid error returned by menu.
func IsInvalidErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrInvalid {
return true
}
return false
}
// IsNoResponseErr checks to see if err is of type no response returned by menu.
func IsNoResponseErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrNoResponse {
return true
}
return false
}
// IsTooManyErr checks to see if err is of type too many returned by menu.
func IsTooManyErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrTooMany {
return true
}
return false
}
// IsDuplicateErr checks to see if err is of type duplicate returned by menu.
func IsDuplicateErr(err error) bool {
e, ok := err.(*MenuError)
if ok && e.Err == ErrDuplicate {
return true
}
return false
}
// IsMenuErr checks to see if it is a menu err.
// This is a general check not a specific one.
func IsMenuErr(err error) bool {
_, ok := err.(*MenuError)
return ok
}