{"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":1447,"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":1429,"sourceCodeEnd":1465,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L1429-L1465","documentation":"Page.On (page.go:1441-1445) subscribes to page events — per the doc comment, 'console' is the only accepted event. The Go mapping layer converts the JavaScript callback into a PageEventHandler; if the argument cannot be converted (null, undefined, or a non-function was passed), handler arrives nil and On rejects it immediately.","triggerScenarios":"page.on('console', null), page.on('console') with the argument omitted entirely, passing a variable that is undefined, or passing a non-function value like a string or object.","commonSituations":"Optional-handler patterns (page.on('console', maybeHandler) where maybeHandler is undefined on some paths); copy-paste from Playwright/Puppeteer examples with different signatures; destructuring config objects that miss the callback key.","solutions":["Always pass a function: page.on('console', msg => console.log(msg.text()))","Guard optional handlers: if (typeof handler === 'function') page.on('console', handler)","Check the event name is exactly 'console' — other names are not accepted by this API","Validate subscribe calls in a helper that asserts typeof fn === 'function' first"],"exampleFix":"// before\npage.on('console', logConsoleMessages); // logConsoleMessages is undefined\n\n// after\nimport { consoleLog } from './helpers.js';\npage.on('console', msg => console.log(msg.text()));","handlingStrategy":"type-guard","validationCode":"if (typeof handler === 'function') {\n  page.on('console', handler);\n} else {\n  throw new Error('page.on requires a function handler');\n}","typeGuard":"function isPageEventHandler(fn) {\n  return typeof fn === 'function';\n}","tryCatchPattern":"try {\n  page.on('console', handler);\n} catch (e) {\n  if (/handler.*cannot be nil/.test(e.message)) {\n    // handler was null/undefined — supply a default and retry\n    page.on('console', msg => console.log(msg.text()));\n  } else throw e;\n}","preventionTips":["Always pass an inline arrow or verified function to page.on","Guard optional callbacks with typeof checks before subscribing","Use only the 'console' event name with this API"],"tags":["browser","events","console","callback"],"backgroundTag":"nil-callback-argument","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}