-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy patherrors.go
106 lines (85 loc) · 2.02 KB
/
errors.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cli
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/labstack/gommon/color"
)
var (
errNotAPointerToStruct = errors.New("not a pointer to struct")
errNotAPointer = errors.New("argv is not a pointer")
errCliTagTooMany = errors.New("cli tag too many")
)
type (
exitError struct{}
commandNotFoundError struct {
command string
}
methodNotAllowedError struct {
method string
}
routerRepeatError struct {
router string
}
wrapError struct {
err error
msg string
}
argvError struct {
isEmpty bool
isOutOfRange bool
ith int
msg string
}
)
func (e exitError) Error() string { return "exit" }
// ExitError is a special error, should be ignored but return
var ExitError = exitError{}
func throwCommandNotFound(command string) commandNotFoundError {
return commandNotFoundError{command: command}
}
func throwMethodNotAllowed(method string) methodNotAllowedError {
return methodNotAllowedError{method: method}
}
func throwRouterRepeat(router string) routerRepeatError {
return routerRepeatError{router: router}
}
func (e commandNotFoundError) Error() string {
return fmt.Sprintf("command %s not found", e.command)
}
func (e methodNotAllowedError) Error() string {
return fmt.Sprintf("method %s not allowed", e.method)
}
func (e routerRepeatError) Error() string {
return fmt.Sprintf("router %s repeat", e.router)
}
func (e wrapError) Error() string {
return e.msg
}
func wrapErr(err error, appendString string, clr color.Color) error {
if err == nil {
return err
}
errs := strings.Split(err.Error(), "\n")
buff := bytes.NewBufferString("")
errPrefix := clr.Red("ERR!") + " "
for i, e := range errs {
if i != 0 {
buff.WriteByte('\n')
}
buff.WriteString(errPrefix)
buff.WriteString(e)
}
buff.WriteString(appendString)
return wrapError{err: err, msg: buff.String()}
}
func (e argvError) Error() string {
if e.isEmpty {
return "argv list is empty"
}
if e.isOutOfRange {
return "argv list out of range"
}
return fmt.Sprintf("%dth argv: %s", e.ith, e.msg)
}