-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
48 lines (40 loc) · 1.24 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
package cli
// ShowHelpError defines a new type of error that defines whenever the error should display the command help before the error message.
type ShowHelpError interface {
error
ShowHelp() bool
}
// NewErrorWithHelp wraps provided error and tells the CLI to show usage help.
func NewErrorWithHelp(err error) error {
return &showHelpError{err: err}
}
type showHelpError struct{ err error }
func (showHelpError) ShowHelp() bool { return true }
func (e showHelpError) Unwrap() error { return e.err }
func (e showHelpError) Error() string {
if e.err != nil {
return e.err.Error()
}
return ""
}
// ExitStatusError defines a new type of error that allow the customization of the CLI exit status.
type ExitStatusError interface {
error
ExitStatus() uint8
}
// NewErrorWithExitStatus wraps the provided error and tells the CLI to exit with provided code.
func NewErrorWithExitStatus(err error, status uint8) error {
return &exitStatusError{err: err, status: status}
}
type exitStatusError struct {
err error
status uint8
}
func (e exitStatusError) ExitStatus() uint8 { return e.status }
func (e exitStatusError) Unwrap() error { return e.err }
func (e exitStatusError) Error() string {
if e.err != nil {
return e.err.Error()
}
return ""
}