grafana/k6 · error

invalid accuracy "%.2f": precondition 0 <= ACCURACY failed

Error message

invalid accuracy "%.2f": precondition 0 <= ACCURACY failed

What it means

Validation error from Geolocation.Validate: the accuracy field of the geolocation override is negative. Accuracy is optional and measured in meters; k6 requires it to be >= 0 when provided (a sensible non-negative precondition matching Playwright's contract).

Source

Thrown at internal/js/modules/k6/browser/common/browser_context_options.go:82

	if p == nil {
		return nil
	}
	if strings.TrimSpace(p.Server) == "" {
		return fmt.Errorf("proxy.server must be set")
	}
	return nil
}

// Validate validates the [Geolocation].
func (g *Geolocation) Validate() error {
	if g.Longitude < -180 || g.Longitude > 180 {
		return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, g.Longitude)
	}
	if g.Latitude < -90 || g.Latitude > 90 {
		return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, g.Latitude)
	}
	if g.Accuracy < 0 {
		return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, g.Accuracy)
	}
	return nil
}

// GrantPermissionsOptions is used by BrowserContext.GrantPermissions.
type GrantPermissionsOptions struct {
	Origin string
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Remove the accuracy field entirely if you don't need it, or pass a non-negative value like accuracy: 100.
  2. Clamp computed values: Math.max(0, accuracy).
  3. Use 0 (or omit) rather than -1 to express 'no accuracy hint'.

Example fix

// before
context.setGeolocation({ latitude: 35.68, longitude: 139.69, accuracy: -1 });

// after
context.setGeolocation({ latitude: 35.68, longitude: 139.69, accuracy: 100 });
// or omit accuracy entirely
Defensive patterns

Strategy: validation

Validate before calling

function isValidAccuracy(a) {
  return a == null || (Number.isFinite(a) && a >= 0);
}
if (!isValidAccuracy(geo.accuracy)) {
  geo = { ...geo, accuracy: Math.max(0, geo.accuracy) }; // or drop the field
}
context.setGeolocation(geo);

Type guard

function hasValidAccuracy(g) {
  return g.accuracy == null || (Number.isFinite(g.accuracy) && g.accuracy >= 0);
}

Prevention

When it happens

Trigger: browser.newContext({ geolocation: { latitude: 10, longitude: 10, accuracy: -1 } }); passing -1 as a sentinel 'unset' value; arithmetic that computes accuracy as a difference which goes negative.

Common situations: Using -1 or 0-minus defaults for optional numeric fields out of habit; computing accuracy from external sensor-like data without clamping; copying fixtures from another system that used -1 for unknown accuracy.

Related errors


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