grafana/k6 · error
"handler" argument cannot be nil
Error message
"handler" argument cannot be nil
What it means
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.
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
Example fix
// before
page.on('console', logConsoleMessages); // logConsoleMessages is undefined
// after
import { consoleLog } from './helpers.js';
page.on('console', msg => console.log(msg.text())); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof handler === 'function') {
page.on('console', handler);
} else {
throw new Error('page.on requires a function handler');
} Type guard
function isPageEventHandler(fn) {
return typeof fn === 'function';
} Try / catch
try {
page.on('console', handler);
} catch (e) {
if (/handler.*cannot be nil/.test(e.message)) {
// handler was null/undefined — supply a default and retry
page.on('console', msg => console.log(msg.text()));
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- browser.on promise rejected
- incorrect event , is the only event supported
- predicate function failed
- unknown browser event
- waitForEvent does not support mapping for event
AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18).
Data as JSON: /api/errors/c0e3a0ac3ca1ab43.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/browser/common/page.go:1447
ConsoleMessage *ConsoleMessage
// Metric is the metric event event.
Metric *MetricEvent
// Request is the read only request that is about to be sent from the
// browser to the WuT.
Request *Request
// Response is the read only response that was received from the WuT.
Response *Response
}
// On subscribes to a page event for which the given handler will be executed
// passing in the ConsoleMessage associated with the event.
// The only accepted event value is 'console'.
func (p *Page) On(event PageEventName, handler PageEventHandler) error {
if handler == nil {
return errors.New(`"handler" argument cannot be nil`)
}
_, err := p.addEventHandler(event, handler)
return err
}
func (p *Page) addEventHandler(event PageEventName, handler PageEventHandler) (id uint64, err error) {
p.eventHandlersMu.Lock()
defer p.eventHandlersMu.Unlock()
r := pageEventHandlerRecord{
id: p.eventHandlerLastID.Add(1),
handler: handler,
}
p.eventHandlers[event] = append(p.eventHandlers[event], r)
return r.id, nil
}View on GitHub (pinned to 3fcf5388d7)