grafana/k6 · error
"handler" argument cannot be nil
Error message
"handler" argument cannot be nil
What it means
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.
Source
Thrown at internal/js/modules/k6/browser/common/page.go:1442
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 93accf6570)
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
Example fix
// before
page.on('console');
// after
page.on('console', (msg) => console.log(msg.text())); Defensive patterns
Strategy: validation
Validate before calling
// Guard before registering
const handler = typeof cb === 'function' ? cb : () => {};
await page.on('console', handler); Type guard
const isPageEventHandler = (h) => typeof h === 'function';
Prevention
- 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
When it happens
Trigger: 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.
Common situations: Copy-paste from docs that drops the callback; optional handlers wired only when a flag is set; refactors that rename the callback function.
Related errors
- predicate function is not callable
- clip area is either empty or outside the viewport
- provided selector is empty
- only one of the selectors can capture using * modifier
- unknown browser event: %q, must be %q
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/c0e3a0ac3ca1ab43.
Report an issue: GitHub.