forked from bpowers/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
54 lines (46 loc) · 1.11 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
package redis
type ErrorFlag uint8
const (
ErrorRedis ErrorFlag = 1 << iota
ErrorConnection
ErrorLoading
ErrorAuth
ErrorParse
ErrorInvalid
ErrorTimeout
)
type Error struct {
msg string
flags ErrorFlag
}
// newError creates a new Error.
func newError(msg string, flags ...ErrorFlag) *Error {
err := new(Error)
err.msg = msg
for _, f := range flags {
err.flags |= f
}
return err
}
// newErrorExt creates a new Error with flags of the given error and
// appends the error message from the given error to the end of the new one.
func newErrorExt(msg string, err *Error, flags ...ErrorFlag) *Error {
return newError(msg+": "+err.Error(), append(flags, err.flags)...)
}
// Error returns a string representation of the error.
func (e *Error) Error() string {
return e.msg
}
// Test returns true, if any of the given error flags is set in the error, otherwise false.
func (e *Error) Test(flags ...ErrorFlag) bool {
for _, f := range flags {
if e.flags&f > 0 {
return true
}
}
return false
}
// errmsg is a helper function for (panic) error messages.
func errmsg(msg string) string {
return "redis: " + msg
}