grafana/k6 · error

handler for event type %q isn't a callable function

Error message

handler for event type %q isn't a callable function

What it means

Nil-guard in addEventListener: the internal registration helper is invoked with a nil handler function for the given event type. End-user scripts hit the equivalent path when the handler passed to ws.addEventListener is not recognized as a function (undefined variable, wrong signature, or a non-callable value); the %q names the event type whose listener failed to register.

Source

Thrown at internal/js/modules/k6/websockets/websockets.go:951

				rt.ToValue(w.closeReason), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
		})
	} else {
		event = w.newEvent(eventType, time.Now())
	}

	for _, listener := range w.eventListeners.all(eventType) {
		if _, err := listener(event); err != nil {
			return err
		}
	}
	return nil
}

func (w *webSocket) addEventListener(event string, handler func(sobek.Value) (sobek.Value, error)) {
	// TODO support options https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters

	if handler == nil {
		common.Throw(w.vu.Runtime(), fmt.Errorf("handler for event type %q isn't a callable function", event))
	}

	if err := w.eventListeners.add(event, handler); err != nil {
		w.vu.State().Logger.Warnf("can't add event handler: %s", err)
	}
}

// TODO add remove listeners

View on GitHub (pinned to 165bb3c1a4)

Solutions

  1. Pass a function reference to addEventListener: ws.addEventListener('open', () => { ... })
  2. Verify the handler variable is defined and not shadowed at the call site
  3. Use arrow functions or named functions instead of the result of a call that returns a non-function
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/js/modules/k6/websockets/websockets.go:926 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@165bb3c1a4 (2026-08-18). Data as JSON: /api/errors/e490ee949c1d9d37. Report an issue: GitHub.