{"record":{"id":"25a75f1f35cd2c91","repo":"hibiken/asynq","slug":"asynq-could-not-parse-redis-uri-database-number","errorCode":null,"errorMessage":"asynq: could not parse redis uri: database number should be the first segment of the path","messagePattern":"asynq: could not parse redis uri: database number should be the first segment of the path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"asynq.go","lineNumber":502,"sourceCode":"\tcase \"redis-socket\":\n\t\treturn parseRedisSocketURI(u)\n\tcase \"redis-sentinel\":\n\t\treturn parseRedisSentinelURI(u)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"asynq: unsupported uri scheme: %q\", u.Scheme)\n\t}\n}\n\nfunc parseRedisURI(u *url.URL) (RedisConnOpt, error) {\n\tvar db int\n\tvar err error\n\tvar redisConnOpt RedisClientOpt\n\n\tif len(u.Path) > 0 {\n\t\txs := strings.Split(strings.Trim(u.Path, \"/\"), \"/\")\n\t\tdb, err = strconv.Atoi(xs[0])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"asynq: could not parse redis uri: database number should be the first segment of the path\")\n\t\t}\n\t}\n\tvar password string\n\tif v, ok := u.User.Password(); ok {\n\t\tpassword = v\n\t}\n\n\tif u.Scheme == \"rediss\" {\n\t\th, _, err := net.SplitHostPort(u.Host)\n\t\tif err != nil {\n\t\t\th = u.Host\n\t\t}\n\t\tredisConnOpt.TLSConfig = &tls.Config{ServerName: h}\n\t}\n\n\tredisConnOpt.Addr = u.Host\n\tredisConnOpt.Password = password\n\tredisConnOpt.DB = db","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/hibiken/asynq/blob/d135f1439bee74e989b7f9b41ecd542cc87f024a/asynq.go#L484-L520","documentation":"When parsing a redis:// or rediss:// URI, the first path segment must be the database number (e.g. /1 in redis://host:6379/1). This error is returned when that segment is not a valid integer. Note this variant is a plain error (no %w), so errors.Is against a sentinel is not possible.","triggerScenarios":"ParseRedisURI(\"redis://host:6379/notanumber\") or any URI whose path starts with a non-numeric segment, e.g. redis://host/index0 or redis://host:6379/extra/path.","commonSituations":"Copy-pasting an HTTP-style path into a redis URI; putting the db in the query string instead of the path; trailing garbage after the db number; using a socket-style path form with the redis:// scheme.","solutions":["Put the numeric database number as the first path segment, e.g. redis://host:6379/0","Remove extra non-numeric path segments or move them to query parameters if intended","If no database selection is needed, omit the path entirely (defaults to db 0)"],"exampleFix":"// before\nopt, err := asynq.ParseRedisURI(\"redis://localhost:6379/db0\")\n// after\nopt, err := asynq.ParseRedisURI(\"redis://localhost:6379/0\")","handlingStrategy":"validation","validationCode":"func validateRedisDBPath(uri string) error {\n    u, err := url.Parse(uri)\n    if err != nil {\n        return err\n    }\n    if u.Path != \"\" {\n        seg := strings.TrimPrefix(u.Path, \"/\")\n        if i := strings.Index(seg, \"/\"); i >= 0 {\n            seg = seg[:i]\n        }\n        if _, err := strconv.Atoi(seg); err != nil {\n            return fmt.Errorf(\"first path segment %q must be a numeric db number\", seg)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"opt, err := asynq.ParseRedisURI(uri)\nif err != nil {\n    if strings.Contains(err.Error(), \"database number should be the first segment\") {\n        return fmt.Errorf(\"redis URI %q: use redis://host:port/<db> with a numeric db\", uri)\n    }\n    return err\n}","preventionTips":["Express the database only as a numeric first path segment (redis://host:6379/0), never by name","Omit the path entirely for db 0","Do not add HTTP-style path components to redis URIs; use query params only for documented keys like master for sentinel URIs"],"tags":["go","redis","uri-parsing","configuration"],"backgroundTag":"invalid-argument-format","analyzedSha":"d135f1439bee74e989b7f9b41ecd542cc87f024a","analyzedAt":"2026-09-07T19:02:34.660Z","contentChangedAt":"2026-09-07T19:02:34.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}