{"record":{"id":"9d7dc47d3bc8635f","repo":"grafana/k6","slug":"failed-to-encode-load-zone-list-w","errorCode":null,"errorMessage":"failed to encode load zone list: %w","messagePattern":"failed to encode load zone list: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/cmd/cloud_load_zone_list.go","lineNumber":77,"sourceCode":"\n\tstackHeader := fmt.Sprintf(\"Load zones for %s:\\n\\n\", cloudStackName(cloudConfig))\n\n\tif len(loadZones) == 0 {\n\t\tprintToStdout(c.globalState, stackHeader+\"No load zones found.\\n\")\n\t\treturn nil\n\t}\n\n\tprintToStdout(c.globalState, stackHeader+formatLoadZoneTable(loadZones))\n\treturn nil\n}\n\nfunc (c *cmdCloudLoadZoneList) outputJSON(loadZones []cloudapiv6.LoadZone) error {\n\tbuf := &bytes.Buffer{}\n\tenc := json.NewEncoder(buf)\n\tenc.SetEscapeHTML(false)\n\tenc.SetIndent(\"\", \"  \")\n\tif err := enc.Encode(loadZones); err != nil {\n\t\treturn fmt.Errorf(\"failed to encode load zone list: %w\", err)\n\t}\n\n\tprintToStdout(c.globalState, buf.String())\n\treturn nil\n}\n\nfunc formatLoadZoneTable(loadZones []cloudapiv6.LoadZone) string {\n\tvar buf strings.Builder\n\tw := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)\n\t_, _ = fmt.Fprintln(w, \"ID\\tNAME\\tTYPE\\tAVAILABLE\")\n\tfor _, z := range loadZones {\n\t\tzoneType := \"private\"\n\t\tif z.Public {\n\t\t\tzoneType = \"public\"\n\t\t}\n\t\tavailable := \"no\"\n\t\tif z.Available {\n\t\t\tavailable = \"yes\"","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/cmd/cloud_load_zone_list.go#L59-L95","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If you maintain a fork, ensure LoadZone fields stay JSON-encodable (strings, ints, bools)","Check the wrapped error — it names the exact encoding fault and offending value","For stock k6, treat this as a bug: report it with the wrapped error at the k6 GitHub tracker","Fall back to the default table output (omit -o json) while investigating"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"for _, z := range loadZones {\n    if z.ID == \"\" { return fmt.Errorf(\"load zone with empty ID; refusing to encode\") }\n}","typeGuard":null,"tryCatchPattern":"if err := enc.Encode(loadZones); err != nil {\n    // table output still works; degrade gracefully rather than failing the command\n    c.globalState.Logger.WithError(err).Warn(\"json encoding failed; falling back to table output\")\n    printToStdout(c.globalState, stackHeader+formatLoadZoneTable(loadZones))\n}","preventionTips":["Keep LoadZone fields restricted to JSON-safe scalar types in forks","Test any struct changes against the JSON encoder before release","Treat encoder errors as non-fatal when a table fallback exists"],"tags":["json","encoding","cli","load-zones","defensive"],"backgroundTag":"json-encoding-failed","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}