redis/internal/errors/errors.go

74 lines
1.1 KiB
Go
Raw Normal View History

2016-07-02 15:52:10 +03:00
package errors
import (
"io"
"net"
"strings"
)
const Nil = RedisError("redis: nil")
type RedisError string
var LoadingError RedisError = "LOADING Redis is loading the dataset in memory"
2016-07-02 15:52:10 +03:00
func (e RedisError) Error() string { return string(e) }
func IsRetryable(err error) bool {
return IsNetwork(err)
}
func IsInternal(err error) bool {
_, ok := err.(RedisError)
return ok
}
func IsNetwork(err error) bool {
if err == io.EOF {
return true
}
_, ok := err.(net.Error)
return ok
}
func IsBadConn(err error, allowTimeout bool) bool {
if err == nil {
return false
}
if IsInternal(err) {
return false
}
if allowTimeout {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return false
}
}
return true
}
func IsMoved(err error) (moved bool, ask bool, addr string) {
if !IsInternal(err) {
return
}
s := err.Error()
if strings.HasPrefix(s, "MOVED ") {
moved = true
} else if strings.HasPrefix(s, "ASK ") {
ask = true
} else {
return
}
2016-09-14 14:00:34 +03:00
ind := strings.LastIndex(s, " ")
2016-07-02 15:52:10 +03:00
if ind == -1 {
return false, false, ""
}
addr = s[ind+1:]
return
}
func IsLoading(err error) bool {
2016-10-09 06:44:58 +03:00
return err.Error() == string(LoadingError)
}