grafana/k6 · error

unmarshalling image format

Error message

unmarshalling image format: %w

What it means

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.

Solutions

  1. Ensure the format field is a JSON string, e.g. "png" or "jpeg".
  2. Validate the JSON payload with a linter before feeding it to the module.
  3. Remember 'bmp'/'webp' won't error here but will decode as zero-value — restrict inputs to png/jpeg explicitly.

Example fix

// before
{"format": png}

// after
{"format": "png"}
Defensive patterns

Strategy: validation

Validate before calling

const FORMATS = ['png', 'jpeg'];
if (!FORMATS.includes(raw)) throw new Error('format must be a quoted "png" or "jpeg"');
const payload = JSON.stringify({ format: raw });

Type guard

const isImageFormat = (v) => v === 'png' || v === 'jpeg';

Prevention

When it happens

Trigger: 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.

Common situations: xk6 extensions or CI tooling that round-trips k6 browser options as JSON; hand-edited option files where quotes were dropped.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/712edac9899e2adf. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/screenshotter.go:67

func ImageIDFromString(format string) (ImageFormat, bool) {
	id, exists := imageFormatToID[format]
	return id, exists
}

// MarshalJSON marshals the enum as a quoted JSON string.
func (f ImageFormat) MarshalJSON() ([]byte, error) {
	buffer := bytes.NewBufferString(`"`)
	buffer.WriteString(imageFormatToString[f])
	buffer.WriteString(`"`)
	return buffer.Bytes(), nil
}

// UnmarshalJSON unmarshals a quoted JSON string to the enum value.
func (f *ImageFormat) UnmarshalJSON(b []byte) error {
	var j string
	err := json.Unmarshal(b, &j)
	if err != nil {
		return fmt.Errorf("unmarshalling image format: %w", err)
	}
	// Note that if the string cannot be found then it will be set to the zero value.
	*f = imageFormatToID[j]
	return nil
}

type screenshotter struct {
	ctx       context.Context
	parentCtx context.Context
	persister ScreenshotPersister
	logger    *log.Logger
}

// Cleanup gets a short independent budget after the operation deadline expires.
const screenshotCleanupTimeout = time.Second

func newScreenshotter(
	ctx context.Context,

View on GitHub (pinned to 3fcf5388d7)