{"record":{"id":"472e80e4c633dbe9","repo":"grafana/k6","slug":"executing-page-on-s-handler-w","errorCode":null,"errorMessage":"executing page.on('%s') handler: %w","messagePattern":"executing page\\.on\\('(.+?)'\\) handler: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/page_mapping.go","lineNumber":803,"sourceCode":"\t\t\tcommon.PageEventMetric:          {mapp: mapMetricEvent, wait: true},\n\t\t\tcommon.PageEventRequest:         {mapp: mapRequestEvent},\n\t\t\tcommon.PageEventResponse:        {mapp: mapResponseEvent},\n\t\t\tcommon.PageEventRequestFinished: {mapp: mapRequestEvent},\n\t\t\tcommon.PageEventRequestFailed:   {mapp: mapRequestEvent},\n\t\t}\n\t\tpageEvent, ok := pageEvents[eventName]\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"unknown page on event: %q\", eventName)\n\t\t}\n\n\t\tctx := vu.Context()\n\t\ttq := vu.get(ctx, p.TargetID())\n\n\t\treturn p.On(eventName, func(event common.PageEvent) error {\n\t\t\twait := queueTask(ctx, tq, func() (sobek.Value, error) {\n\t\t\t\t_, err := handle(sobek.Undefined(), vu.Runtime().ToValue(pageEvent.mapp(vu, event)))\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, fmt.Errorf(\"executing page.on('%s') handler: %w\", eventName, err)\n\t\t\t\t}\n\t\t\t\treturn nil, nil\n\t\t\t})\n\t\t\tif pageEvent.wait {\n\t\t\t\tif _, err := wait(); errors.Is(err, context.Canceled) {\n\t\t\t\t\treturn errors.New(\"iteration ended before page.on handler completed executing\")\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\t}\n}\n\nfunc parseWaitForFunctionArgs(\n\trt *sobek.Runtime, timeout time.Duration, pageFunc, opts sobek.Value, gargs ...sobek.Value,\n) (string, *common.FrameWaitForFunctionOptions, []any, error) {\n\tpopts, err := parseFrameWaitForFunctionOptions(rt, opts, timeout)\n\tif err != nil {","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/browser/page_mapping.go#L785-L821","documentation":"Produced inside the page.on(eventName, handler) callback wrapper when the user-supplied JavaScript handler throws. The handler is executed through the per-target task queue with the mapped event object; any exception it raises is wrapped with the event name and returned as the handler's error to the browser event dispatch layer.","triggerScenarios":"A page.on('response', handler) body calling methods that do not exist on the mapped Response, dereferencing null (e.g. r.request() chain assumptions), or any JS exception (TypeError, ReferenceError) inside the handler when a matching event fires.","commonSituations":"Handlers written against the Playwright object model where k6's mapping differs; handlers that assume every response has a body or request available synchronously; forgetting that the handler runs for every matching event, including preflight OPTIONS and failed requests.","solutions":["Wrap the handler body in try/catch and swallow or log errors so one event cannot break the run.","Guard optional accessors: if (!r.response()) return; before using response fields.","Test the handler against the documented mapped objects (k6 Response/Request/ConsoleMessage) rather than Playwright's full API.","Keep handlers cheap and synchronous; never await long operations inside page.on handlers."],"exampleFix":"// before\npage.on('response', r => console.log(r.status(), r.request().method()));\n\n// after\npage.on('response', r => {\n  try { console.log(r.status(), r.request().method()); }\n  catch (e) { console.log('handler error:', e.message); }\n});","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"page.on('response', (r) => {\n  try { console.log(r.status()); }\n  catch (e) { console.log('page.on handler failed:', e.message); }\n});","preventionTips":["Wrap every page.on handler body in try/catch so a single bad event cannot break the run.","Guard optional accessors (response(), request()) before dereferencing.","Keep handlers synchronous and cheap; do heavy work after the run via metrics/logs."],"tags":["k6","browser","page","event-listener","callback-error"],"backgroundTag":"runtime-error-in-callback","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}