{"record":{"id":"c30d7edf35c7f2cb","repo":"redis/go-redis","slug":"redis-invalid-database-number-q-c30d7e","errorCode":null,"errorMessage":"redis: invalid database number: %q","messagePattern":"redis: invalid database number: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sentinel.go","lineNumber":453,"sourceCode":"\tswitch u.Scheme {\n\tcase \"rediss\":\n\t\to.TLSConfig = &tls.Config{ServerName: h, MinVersion: tls.VersionTLS12}\n\tcase \"redis\":\n\t\to.TLSConfig = nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"redis: invalid URL scheme: %s\", u.Scheme)\n\t}\n\n\tf := strings.FieldsFunc(u.Path, func(r rune) bool {\n\t\treturn r == '/'\n\t})\n\tswitch len(f) {\n\tcase 0:\n\t\to.DB = 0\n\tcase 1:\n\t\tvar err error\n\t\tif o.DB, err = strconv.Atoi(f[0]); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"redis: invalid database number: %q\", f[0])\n\t\t}\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"redis: invalid URL path: %s\", u.Path)\n\t}\n\n\treturn setupFailoverConnParams(u, o)\n}\n\nfunc setupFailoverConnParams(u *url.URL, o *FailoverOptions) (*FailoverOptions, error) {\n\tq := queryOptions{q: u.Query()}\n\n\to.MasterName = q.string(\"master_name\")\n\to.ClientName = q.string(\"client_name\")\n\to.RouteByLatency = q.bool(\"route_by_latency\")\n\to.RouteByLatencyTolerance = q.duration(\"route_by_latency_tolerance\")\n\to.RouteRandomly = q.bool(\"route_randomly\")\n\to.ReplicaOnly = q.bool(\"replica_only\")\n\to.UseDisconnectedReplicas = q.bool(\"use_disconnected_replicas\")","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/redis/go-redis/blob/c5cad058c72f58370553b48566302303cf8a2e89/sentinel.go#L435-L471","documentation":"ParseFailoverURL's path component must be a single integer naming the Redis database index. When the path is non-empty but cannot be parsed as an integer (strconv.Atoi fails), setupFailoverConn returns this error wrapping the offending segment.","triggerScenarios":"Calling ParseFailoverURL with a path like \"/mydb\", \"/0/master\", or \"/01a\" — anything that is present but not a pure integer.","commonSituations":"Putting the master name in the path (it belongs in the masterName option or as a query/other field), or pasting a URL where the path holds a database *name* rather than a numeric index.","solutions":["Use a numeric database in the path: redis://host:26379/2","Remove the path entirely if DB 0 is fine (empty path defaults to DB 0)","Move the master name to FailoverOptions.MasterName instead of the URL path"],"exampleFix":"// before\nopt, err := redis.ParseFailoverURL(\"redis://myapp-db\")\n// after\nopt, err := redis.ParseFailoverURL(\"redis://myapp-db/0\")","handlingStrategy":"validation","validationCode":"u, err := url.Parse(raw)\nif err != nil { return err }\np := strings.TrimPrefix(u.Path, \"/\")\nif p != \"\" {\n    if _, err := strconv.Atoi(p); err != nil { return fmt.Errorf(\"path must be numeric DB index, got %q\", p) }\n}","typeGuard":null,"tryCatchPattern":"opt, err := redis.ParseFailoverURL(raw)\nif err != nil {\n    if strings.Contains(err.Error(), \"invalid database number\") { /* fix URL path */ }\n    return err\n}","preventionTips":["Never put the master name in the URL path","Use only a numeric DB segment: /0, /1, ...","Prefer setting DB via FailoverOptions struct for non-numeric cases"],"tags":["redis","sentinel","url-parsing"],"backgroundTag":"invalid-url-scheme","analyzedSha":"c5cad058c72f58370553b48566302303cf8a2e89","analyzedAt":"2026-09-01T06:50:53.388Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}