{"record":{"id":"712edac9899e2adf","repo":"grafana/k6","slug":"unmarshalling-image-format-w","errorCode":null,"errorMessage":"unmarshalling image format: %w","messagePattern":"unmarshalling image format: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":67,"sourceCode":"func ImageIDFromString(format string) (ImageFormat, bool) {\n\tid, exists := imageFormatToID[format]\n\treturn id, exists\n}\n\n// MarshalJSON marshals the enum as a quoted JSON string.\nfunc (f ImageFormat) MarshalJSON() ([]byte, error) {\n\tbuffer := bytes.NewBufferString(`\"`)\n\tbuffer.WriteString(imageFormatToString[f])\n\tbuffer.WriteString(`\"`)\n\treturn buffer.Bytes(), nil\n}\n\n// UnmarshalJSON unmarshals a quoted JSON string to the enum value.\nfunc (f *ImageFormat) UnmarshalJSON(b []byte) error {\n\tvar j string\n\terr := json.Unmarshal(b, &j)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unmarshalling image format: %w\", err)\n\t}\n\t// Note that if the string cannot be found then it will be set to the zero value.\n\t*f = imageFormatToID[j]\n\treturn nil\n}\n\ntype screenshotter struct {\n\tctx       context.Context\n\tparentCtx context.Context\n\tpersister ScreenshotPersister\n\tlogger    *log.Logger\n}\n\n// Cleanup gets a short independent budget after the operation deadline expires.\nconst screenshotCleanupTimeout = time.Second\n\nfunc newScreenshotter(\n\tctx context.Context,","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/screenshotter.go#L49-L85","documentation":"Returned by ImageFormat.UnmarshalJSON when the JSON payload for a screenshot format is not a quoted string (e.g. a bare number, an object, or malformed JSON). k6 serializes screenshot options internally, so end users normally never trigger this; it appears when tooling or extensions feed JSON into the browser module's option structs.","triggerScenarios":"Unmarshalling JSON like {\"format\": 3} or {\"format\": [\"png\"]} into a struct with ImageFormat; truncated JSON from a pipe or file. Note: an unknown but well-quoted string (\"bmp\") does NOT error — it silently becomes the zero value, which is a separate pitfall.","commonSituations":"xk6 extensions or CI tooling that round-trips k6 browser options as JSON; hand-edited option files where quotes were dropped.","solutions":["Ensure the format field is a JSON string, e.g. \"png\" or \"jpeg\".","Validate the JSON payload with a linter before feeding it to the module.","Remember 'bmp'/'webp' won't error here but will decode as zero-value — restrict inputs to png/jpeg explicitly."],"exampleFix":"// before\n{\"format\": png}\n\n// after\n{\"format\": \"png\"}","handlingStrategy":"validation","validationCode":"const FORMATS = ['png', 'jpeg'];\nif (!FORMATS.includes(raw)) throw new Error('format must be a quoted \"png\" or \"jpeg\"');\nconst payload = JSON.stringify({ format: raw });","typeGuard":"const isImageFormat = (v) => v === 'png' || v === 'jpeg';","tryCatchPattern":null,"preventionTips":["Always produce format via JSON.stringify so it is a quoted string.","Reject unknown strings before unmarshalling — they silently decode to zero value otherwise."],"tags":["k6","browser","json","image-format","screenshot"],"backgroundTag":"json-deserialization-error","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}