grafana/k6 · warning

failed to encode load zone list

Error message

failed to encode load zone list: %w

What it means

cmdCloudLoadZoneList.outputJSON failed while encoding the fetched load-zone slice with encoding/json before printing it to stdout. json.Encoder.Encode on a []cloudapiv6.LoadZone of plain scalar fields is practically incapable of failing, so this path is a defensive guard; if it ever fires, the wrapped error indicates a serializer-level problem (e.g. unsupported values injected into the slice) rather than an API issue — the load zones were already retrieved successfully.

Solutions

  1. If you maintain a fork, ensure LoadZone fields stay JSON-encodable (strings, ints, bools)
  2. Check the wrapped error — it names the exact encoding fault and offending value
  3. For stock k6, treat this as a bug: report it with the wrapped error at the k6 GitHub tracker
  4. Fall back to the default table output (omit -o json) while investigating
Defensive patterns

Strategy: try-catch

Validate before calling

for _, z := range loadZones {
    if z.ID == "" { return fmt.Errorf("load zone with empty ID; refusing to encode") }
}

Try / catch

if err := enc.Encode(loadZones); err != nil {
    // table output still works; degrade gracefully rather than failing the command
    c.globalState.Logger.WithError(err).Warn("json encoding failed; falling back to table output")
    printToStdout(c.globalState, stackHeader+formatLoadZoneTable(loadZones))
}

Prevention

When it happens

Trigger: A LoadZone struct altered to carry unencodable field types (func, chan, NaN with a custom encoder), a nil slice passed through a code path that panics elsewhere, or an encoder misconfigured with incompatible writer state. Standard usage (`k6 cloud load-zones list -o json`) cannot trigger it with released field types.

Common situations: Forks/extensions adding fields of unsupported types to LoadZone, or output redirection where the underlying writer fails (which surfaces as a different error). Effectively only seen in custom builds.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/9d7dc47d3bc8635f. Report an issue: GitHub.

Appendix: source

Thrown at internal/cmd/cloud_load_zone_list.go:77

	stackHeader := fmt.Sprintf("Load zones for %s:\n\n", cloudStackName(cloudConfig))

	if len(loadZones) == 0 {
		printToStdout(c.globalState, stackHeader+"No load zones found.\n")
		return nil
	}

	printToStdout(c.globalState, stackHeader+formatLoadZoneTable(loadZones))
	return nil
}

func (c *cmdCloudLoadZoneList) outputJSON(loadZones []cloudapiv6.LoadZone) error {
	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", "  ")
	if err := enc.Encode(loadZones); err != nil {
		return fmt.Errorf("failed to encode load zone list: %w", err)
	}

	printToStdout(c.globalState, buf.String())
	return nil
}

func formatLoadZoneTable(loadZones []cloudapiv6.LoadZone) string {
	var buf strings.Builder
	w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
	_, _ = fmt.Fprintln(w, "ID\tNAME\tTYPE\tAVAILABLE")
	for _, z := range loadZones {
		zoneType := "private"
		if z.Public {
			zoneType = "public"
		}
		available := "no"
		if z.Available {
			available = "yes"

View on GitHub (pinned to 01ffac6f24)