{"record":{"id":"e490ee949c1d9d37","repo":"grafana/k6","slug":"handler-for-event-type-q-isn-t-a-callable-functio","errorCode":null,"errorMessage":"handler for event type %q isn't a callable function","messagePattern":"handler for event type %q isn't a callable function","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/websockets.go","lineNumber":951,"sourceCode":"\t\t\t\trt.ToValue(w.closeReason), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))\n\t\t})\n\t} else {\n\t\tevent = w.newEvent(eventType, time.Now())\n\t}\n\n\tfor _, listener := range w.eventListeners.all(eventType) {\n\t\tif _, err := listener(event); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (w *webSocket) addEventListener(event string, handler func(sobek.Value) (sobek.Value, error)) {\n\t// TODO support options https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters\n\n\tif handler == nil {\n\t\tcommon.Throw(w.vu.Runtime(), fmt.Errorf(\"handler for event type %q isn't a callable function\", event))\n\t}\n\n\tif err := w.eventListeners.add(event, handler); err != nil {\n\t\tw.vu.State().Logger.Warnf(\"can't add event handler: %s\", err)\n\t}\n}\n\n// TODO add remove listeners\n","sourceCodeStart":933,"sourceCodeEnd":960,"githubUrl":"https://github.com/grafana/k6/blob/165bb3c1a492b7a4823907fe1e93a30645794737/internal/js/modules/k6/websockets/websockets.go#L933-L960","documentation":"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.","triggerScenarios":"Thrown at internal/js/modules/k6/websockets/websockets.go:926 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a function reference to addEventListener: ws.addEventListener('open', () => { ... })","Verify the handler variable is defined and not shadowed at the call site","Use arrow functions or named functions instead of the result of a call that returns a non-function"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"165bb3c1a492b7a4823907fe1e93a30645794737","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}