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

  1. Always pass a function: page.on('console', msg => console.log(msg.text()))
  2. Guard optional handlers: if (typeof handler === 'function') page.on('console', handler)
  3. Check the event name is exactly 'console' — other names are not accepted by this API
  4. 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

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


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)