crowdsecurity/crowdsec · error
failed to build hub index request: %w
Error message
failed to build hub index request: %w
What it means
FetchIndex wraps the error returned by Downloader.urlTo when it cannot construct the URL for the hub index file (.index.json). This means the Downloader's base URL was missing or malformed, so no HTTP request was even attempted. The wrap preserves the underlying cause.
Source
Thrown at pkg/cwhub/download.go:59
raw := fmt.Sprintf(d.URLTemplate, d.Branch, remotePath)
parsed, err := url.Parse(raw)
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %w", err)
}
return parsed, nil
}
// FetchIndex downloads the index from the hub and writes it to the filesystem.
// It uses a temporary file to avoid partial downloads, and won't overwrite the original
// if it has not changed.
// Return true if the file has been updated, false if already up to date.
func (d *Downloader) FetchIndex(ctx context.Context, destPath string, withContent bool, logger *logrus.Logger) (downloaded bool, err error) {
url, err := d.urlTo(".index.json")
if err != nil {
return false, fmt.Errorf("failed to build hub index request: %w", err)
}
if withContent {
q := url.Query()
q.Set("with_content", "true")
url.RawQuery = q.Encode()
}
downloaded, err = downloader.
New().
WithHTTPClient(HubClient).
ToFile(destPath).
WithETagFn(downloader.SHA256).
CompareContent().
WithLogger(logger.WithField("url", url)).
BeforeRequest(func(_ *http.Request) {
fmt.Fprintln(os.Stdout, "Downloading " + destPath)
}).View on GitHub (pinned to 909b515798)
Solutions
- Check the `api_url` (hub) setting in your crowdsec config file is set and is a valid absolute URL
- Run `cscli hub update -o raw` or inspect `cscli config show` to see the hub URL in use
- Fix the config value or restore the default hub URL, then retry `cscli hub update`
Example fix
// before (config.yaml)
api:
hub:
api_url: ""
// after
api:
hub:
api_url: "https://hub.crowdsec.net" Defensive patterns
Strategy: try-catch
Validate before calling
if u, err := url.Parse(cfg.HubAPIURL); err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("hub api_url %q is not a valid absolute URL", cfg.HubAPIURL)
} Try / catch
downloaded, err := d.FetchIndex(ctx, dest, withContent, logger)
var urlErr *url.Error
if errors.As(err, &urlErr) {
logger.Errorf("hub URL invalid: %v — check api_url in config", urlErr)
return
} Prevention
- Always set a valid absolute hub api_url in config
- Validate config URLs at startup before any hub operation
- Smoke-test with `cscli hub update` after config changes
When it happens
Trigger: Calling Downloader.FetchIndex when the downloader has no valid base API URL configured (empty HubIndex URL, or urlTo fails to parse the resulting URL string).
Common situations: Hub URL misconfigured in config.yaml / dev.yaml (empty or typo'd api_url), environment where the hub URL was overridden to an invalid string, or a build/config migration that left the URL unset.
Understand the failure class
Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.
Related errors
- failed to build request: %w
- invalid URL template '%s'
- failed to parse URL: %w
- empty loki host
- no hub configuration provided
AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06).
Data as JSON: /api/errors/eaf8093a0516c789.
Report an issue: GitHub.