grafana/k6 · error

parsing urls: %w

Error message

parsing urls: %w

What it means

Thrown by filterCookies (reached via BrowserContext.Cookies) when the URL list cannot be prepared: parseURLs returns an error and it is wrapped as 'parsing urls'. This is a pure input-validation failure — the cookie data itself was already retrieved successfully.

Source

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

		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)
	}

	// the following algorithm is like a sorting algorithm,
	// but instead of sorting, it filters the cookies slice
	// in place, without allocating a new slice. this is
	// done to avoid unnecessary allocations and copying
	// of data.
	//
	// n is used to remember the last cookie that should be
	// kept in the cookies slice. all cookies before n should
	// be kept, all cookies after n should be removed. it
	// constantly shifts cookies to be kept to the left in the
	// slice, overwriting cookies that should be removed.
	//
	// if a cookie should not be kept, it will be overwritten
	// by the next cookie that should be kept. if no cookies
	// should be kept, a nil slice is returned. otherwise,
	// the slice is truncated to the last cookie that should

View on GitHub (pinned to 93accf6570)

Solutions

  1. Give every URL a scheme: 'https://example.com/path' rather than 'example.com/path'.
  2. Sanitize the list up front: trim strings, prefix a scheme when missing, drop empties.
  3. Log the offending URL — the nested error includes it in quotes.

Example fix

// before
const urls = ['example.com', 'http://ok.example'];
context.cookies(...urls); // first entry fails

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

Strategy: validation

Validate before calling

function toAbsoluteUrl(u) {
  const t = String(u).trim();
  return /^https?:\/\//.test(t) ? t : `https://${t}`;
}
context.cookies(urls.map(toAbsoluteUrl));

Prevention

When it happens

Trigger: browserContext.cookies(urls) with any URL string that url.ParseRequestURI rejects: missing scheme (plain 'example.com'), spaces or control characters, 'http://exa mple.com', or an empty-after-trim value. Note the implementation trims whitespace before parsing, so only genuinely malformed URLs fail.

Common situations: Dynamic URL lists assembled from config, CSV/JSON data, or env variables that contain hostnames instead of URLs; copy-paste artifacts like quotes or trailing commas inside the URL string.

Related errors


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