grafana/k6 · error

filtering cookies: %w

Error message

filtering cookies: %w

What it means

Thrown by BrowserContext.Cookies when post-retrieval URL filtering fails. After the CDP cookies are fetched and converted, filterCookies parses the caller-supplied URLs to match cookies against them; any failure in that step is wrapped as 'filtering cookies'. In practice the wrapped cause is always a URL parsing error.

Source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:560

	// convert the received CDP cookies to the browser API format.
	cookies := make([]*Cookie, len(networkCookies))
	for i, c := range networkCookies {
		cookies[i] = &Cookie{
			Name:     c.Name,
			Value:    c.Value,
			Domain:   c.Domain,
			Path:     c.Path,
			Expires:  int64(c.Expires),
			HTTPOnly: c.HTTPOnly,
			Secure:   c.Secure,
			SameSite: CookieSameSite(c.SameSite),
		}
	}
	// filter cookies by the provided URLs.
	cookies, err = filterCookies(cookies, urls...)
	if err != nil {
		return nil, fmt.Errorf("filtering cookies: %w", err)
	}
	if len(cookies) == 0 {
		return nil, nil
	}

	return cookies, nil
}

// filterCookies filters the given cookies based on URLs.
// If an error occurs while parsing the cookie URLs, the error is returned.
func filterCookies(cookies []*Cookie, urls ...string) ([]*Cookie, error) {
	if len(urls) == 0 || len(cookies) == 0 {
		return cookies, nil
	}

	purls, err := parseURLs(urls...)
	if err != nil {
		return nil, fmt.Errorf("parsing urls: %w", err)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass fully-qualified URLs with scheme: context.cookies(['https://example.com']).
  2. Normalize/validate the URL list before calling cookies().
  3. Read the inner error message — it names the exact offending URL (see the '%q: %w' parse error).

Example fix

// before
const cookies = context.cookies(['example.com']); // no scheme -> error

// after
const cookies = context.cookies(['https://example.com']);
Defensive patterns

Strategy: validation

Validate before calling

const validUrls = urls.map((u) => u.trim()).filter((u) => /^https?:\/\/\S+$/.test(u));
if (validUrls.length !== urls.length) {
  throw new Error('all URLs must be absolute, e.g. https://example.com');
}
context.cookies(validUrls);

Prevention

When it happens

Trigger: Calling browserContext.cookies(urls) where one of the URLs is not parseable by Go's url.ParseRequestURI — e.g. 'example.com' with no scheme, a string with spaces, or garbage input. The CDP part already succeeded; only the filtering stage failed.

Common situations: Passing bare hostnames instead of full URLs; constructing the URL list from user input or env vars without normalizing; trailing whitespace or stray characters in configured URLs.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/45d45dc261151427. Report an issue: GitHub.