{"record":{"id":"c0e3a0ac3ca1ab43","repo":"grafana/k6","slug":"handler-argument-cannot-be-nil","errorCode":null,"errorMessage":"\"handler\" argument cannot be nil","messagePattern":"\"handler\" argument cannot be nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":1442,"sourceCode":"\tConsoleMessage *ConsoleMessage\n\n\t// Metric is the metric event event.\n\tMetric *MetricEvent\n\n\t// Request is the read only request that is about to be sent from the\n\t// browser to the WuT.\n\tRequest *Request\n\n\t// Response is the read only response that was received from the WuT.\n\tResponse *Response\n}\n\n// On subscribes to a page event for which the given handler will be executed\n// passing in the ConsoleMessage associated with the event.\n// The only accepted event value is 'console'.\nfunc (p *Page) On(event PageEventName, handler PageEventHandler) error {\n\tif handler == nil {\n\t\treturn errors.New(`\"handler\" argument cannot be nil`)\n\t}\n\n\t_, err := p.addEventHandler(event, handler)\n\treturn err\n}\n\nfunc (p *Page) addEventHandler(event PageEventName, handler PageEventHandler) (id uint64, err error) {\n\tp.eventHandlersMu.Lock()\n\tdefer p.eventHandlersMu.Unlock()\n\n\tr := pageEventHandlerRecord{\n\t\tid:      p.eventHandlerLastID.Add(1),\n\t\thandler: handler,\n\t}\n\tp.eventHandlers[event] = append(p.eventHandlers[event], r)\n\n\treturn r.id, nil\n}","sourceCodeStart":1424,"sourceCodeEnd":1460,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/page.go#L1424-L1460","documentation":"Page.On (page.go:1437-1446) registers a handler for page events; currently 'console' is the only accepted event. A nil handler is rejected immediately with '\"handler\" argument cannot be nil' — there is nothing sensible to invoke on the event.","triggerScenarios":"page.on('console') without a second argument; passing a null/undefined variable as the handler (e.g. a callback assigned conditionally); destructuring or renaming that leaves the handler undefined.","commonSituations":"Copy-paste from docs that drops the callback; optional handlers wired only when a flag is set; refactors that rename the callback function.","solutions":["Always pass a function as the second argument to page.on()","If the handler is optional in your harness, default it to a no-op: page.on('console', handler || (() => {}))","Check for typos in the handler variable name before registering"],"exampleFix":"// before\npage.on('console');\n\n// after\npage.on('console', (msg) => console.log(msg.text()));","handlingStrategy":"validation","validationCode":"// Guard before registering\nconst handler = typeof cb === 'function' ? cb : () => {};\nawait page.on('console', handler);","typeGuard":"const isPageEventHandler = (h) => typeof h === 'function';","tryCatchPattern":null,"preventionTips":["Always pass a concrete callback as the second argument to page.on()","Default optional handlers to a no-op function","Use typeof checks when handlers come from configuration or shared helpers"],"tags":["browser","event-handling","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}