-
Notifications
You must be signed in to change notification settings - Fork 10
/
errors.go
81 lines (69 loc) · 1.89 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
package meta
type Errorable interface {
// Errorable should be a go error
error
// ErrorKind is a useless method, and probably a poor design of this. My goal is a recursive Errorable type which is an ErrorHash OR ErrorAtom OR ErrorSlice
ErrorKind() string
}
// assert Errorable at compile-time
var (
_ Errorable = ErrorHash(nil)
_ Errorable = ErrorAtom("")
_ Errorable = ErrorSlice(nil)
)
type ErrorHash map[string]Errorable
type ErrorAtom string
type ErrorSlice []Errorable
func (e ErrorAtom) ErrorKind() string {
return string(e)
}
func (eh ErrorHash) ErrorKind() string {
return "invalid_hash"
}
func (ea ErrorSlice) ErrorKind() string {
return "invalid_slice"
}
func (e ErrorAtom) Error() string {
return string(e)
}
func (eh ErrorHash) Error() string {
j, _ := MetaJson.Marshal(eh)
return string(j)
}
func (ea ErrorSlice) Error() string {
return "invalid_slice"
}
// Len returns the number of non-nil error
func (ea ErrorSlice) Len() (n int) {
for _, e := range ea {
if e != nil {
n++
}
}
return
}
// NewHash returns a new hash with a single key/value in it, eg {"error": "too_big"}
func NewHash(key, value string) ErrorHash {
return ErrorHash{key: ErrorAtom(value)}
}
var (
ErrMalformed = ErrorAtom("malformed_json")
ErrBlank = ErrorAtom("blank")
ErrRequired = ErrorAtom("required")
ErrMinRunes = ErrorAtom("min_runes")
ErrMaxRunes = ErrorAtom("max_runes")
ErrMaxBytes = ErrorAtom("max_bytes")
ErrUtf8 = ErrorAtom("utf8")
ErrBool = ErrorAtom("bool")
ErrTime = ErrorAtom("time")
ErrInt = ErrorAtom("int")
ErrIntRange = ErrorAtom("int_range")
ErrString = ErrorAtom("string")
ErrFloat = ErrorAtom("float")
ErrFloatRange = ErrorAtom("float_range")
ErrMin = ErrorAtom("min")
ErrMax = ErrorAtom("max")
ErrIn = ErrorAtom("in")
ErrMinLength = ErrorAtom("min_length")
ErrMaxLength = ErrorAtom("max_length")
)