Add redis prefix to errors

This commit is contained in:
Vladimir Mihailenco 2020-09-28 15:29:35 +03:00
parent d31cf6c328
commit 9e4063ab5a
2 changed files with 13 additions and 12 deletions

View File

@ -206,7 +206,7 @@ func ParseURL(redisURL string) (*Options, error) {
case "unix":
return setupUnixConn(u)
default:
return nil, fmt.Errorf("invalid redis URL scheme: %s", u.Scheme)
return nil, fmt.Errorf("redis: invalid URL scheme: %s", u.Scheme)
}
}
@ -216,7 +216,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
o.Username, o.Password = getUserPassword(u)
if len(u.Query()) > 0 {
return nil, errors.New("no options supported")
return nil, errors.New("redis: no options supported")
}
h, p, err := net.SplitHostPort(u.Host)
@ -239,10 +239,10 @@ func setupTCPConn(u *url.URL) (*Options, error) {
o.DB = 0
case 1:
if o.DB, err = strconv.Atoi(f[0]); err != nil {
return nil, fmt.Errorf("invalid redis database number: %q", f[0])
return nil, fmt.Errorf("redis: invalid database number: %q", f[0])
}
default:
return nil, fmt.Errorf("invalid redis URL path: %s", u.Path)
return nil, fmt.Errorf("redis: invalid URL path: %s", u.Path)
}
if u.Scheme == "rediss" {
@ -258,7 +258,7 @@ func setupUnixConn(u *url.URL) (*Options, error) {
}
if strings.TrimSpace(u.Path) == "" { // path is required with unix connection
return nil, errors.New("empty redis unix socket path")
return nil, errors.New("redis: empty unix socket path")
}
o.Addr = u.Path
@ -268,9 +268,10 @@ func setupUnixConn(u *url.URL) (*Options, error) {
if dbStr == "" {
return o, nil // if database is not set, connect to 0 db.
}
db, err := strconv.Atoi(dbStr)
if err != nil {
return nil, fmt.Errorf("invalid reids database number: %s", err)
return nil, fmt.Errorf("redis: invalid database number: %s", err)
}
o.DB = db

View File

@ -87,37 +87,37 @@ func TestParseURL(t *testing.T) {
{
"unix://foo:bar@/tmp/redis.sock?db=test",
"/tmp/redis.sock",
0, false, errors.New("invalid reids database number: strconv.Atoi: parsing \"test\": invalid syntax"),
0, false, errors.New("redis: invalid database number: strconv.Atoi: parsing \"test\": invalid syntax"),
"", "",
},
{
"redis://localhost/?abc=123",
"",
0, false, errors.New("no options supported"),
0, false, errors.New("redis: no options supported"),
"", "",
},
{
"http://google.com",
"",
0, false, errors.New("invalid redis URL scheme: http"),
0, false, errors.New("redis: invalid URL scheme: http"),
"", "",
},
{
"redis://localhost/1/2/3/4",
"",
0, false, errors.New("invalid redis URL path: /1/2/3/4"),
0, false, errors.New("redis: invalid URL path: /1/2/3/4"),
"", "",
},
{
"12345",
"",
0, false, errors.New("invalid redis URL scheme: "),
0, false, errors.New("redis: invalid URL scheme: "),
"", "",
},
{
"redis://localhost/iamadatabase",
"",
0, false, errors.New(`invalid redis database number: "iamadatabase"`),
0, false, errors.New(`redis: invalid database number: "iamadatabase"`),
"", "",
},
}