grafana/k6 · error

invalid type %T, it needs to be a string or ArrayBuffer

Error message

invalid type %T, it needs to be a string or ArrayBuffer

What it means

A JS API expected data convertible to an io.Reader (string, []byte, ArrayBuffer or a reader), but the exported value had another Go type. GetReader's default branch names the actual type; used e.g. for summary/external data input.

Source

Thrown at js/common/util.go:32

	if e, ok := err.(*sobek.Exception); ok { //nolint:errorlint // we don't really want to unwrap here
		panic(e)
	}
	panic(rt.NewGoError(err)) // this catches the stack unlike rt.ToValue
}

// GetReader tries to return an io.Reader value from an exported Sobek value.
func GetReader(data any) (io.Reader, error) {
	switch r := data.(type) {
	case string:
		return bytes.NewBufferString(r), nil
	case []byte:
		return bytes.NewBuffer(r), nil
	case io.Reader:
		return r, nil
	case sobek.ArrayBuffer:
		return bytes.NewBuffer(r.Bytes()), nil
	default:
		return nil, fmt.Errorf("invalid type %T, it needs to be a string or ArrayBuffer", data)
	}
}

// ToBytes tries to return a byte slice from compatible types.
func ToBytes(data any) ([]byte, error) {
	switch dt := data.(type) {
	case []byte:
		return dt, nil
	case string:
		return []byte(dt), nil
	case sobek.ArrayBuffer:
		return dt.Bytes(), nil
	default:
		return nil, fmt.Errorf("invalid type %T, expected string, []byte or ArrayBuffer", data)
	}
}

// ToString tries to return a string from compatible types.

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Pass a string or ArrayBuffer instead of the unsupported type
  2. Convert the value in the script, e.g. JSON.stringify objects before passing them
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at js/common/util.go:32 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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