{"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":66,"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\tpersister ScreenshotPersister\n\tlogger    *log.Logger\n}\n\nfunc newScreenshotter(\n\tctx context.Context,\n\tsp ScreenshotPersister,\n\tlogger *log.Logger,\n) *screenshotter {\n\treturn &screenshotter{ctx, sp, logger}","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/screenshotter.go#L48-L84","documentation":"ImageFormat.UnmarshalJSON (used when decoding screenshot options from JSON) requires a JSON string; if the raw bytes are not a valid JSON string (e.g., a number, object, or malformed JSON), encoding/json fails and is wrapped here. Note that an unknown-but-valid string silently maps to the zero value — this error is strictly about JSON structure, not about the format name.","triggerScenarios":"Decoding screenshot configuration JSON where format is unquoted (format: png instead of \"png\"), a number, or otherwise syntactically invalid JSON. Reached programmatically (Go/embedding or JSON config paths), not through the normal JS page.screenshot options object.","commonSituations":"Hand-written JSON configs for screenshot options; integrations that template JSON and drop quotes; passing raw user input as option JSON.","solutions":["Ensure format is a quoted JSON string, e.g., \"format\": \"png\" (or \"jpeg\")","Validate/serialize the options with a JSON encoder instead of string concatenation","Reject non-string format values at the config boundary before decoding"],"exampleFix":"// before\n{\"format\": png}\n\n// after\n{\"format\": \"png\"}","handlingStrategy":"validation","validationCode":"const raw = JSON.parse(cfgJson);\nif (typeof raw.format !== 'undefined' && typeof raw.format !== 'string') throw new Error('format must be a string');","typeGuard":"const isImageFormat = (v) => v === undefined || v === 'png' || v === 'jpeg' || v === 'webp';","tryCatchPattern":"try {\n  const fmt = JSON.parse(jsonBytes); // or Go: json.Unmarshal into ImageFormat\n} catch (e) {\n  if (/unmarshalling image format/.test(e.message)) { /* fix JSON quoting, not the value name */ }\n  else throw e;\n}","preventionTips":["Generate screenshot-option JSON with a real serializer; never concatenate strings","Quote enum values in JSON configs (\"png\", not png)","Validate decoded shapes at the config boundary"],"tags":["browser","screenshot","json","serialization","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}