{"record":{"id":"d488c45a3882a634","repo":"grafana/k6","slug":"page-route-s-w","errorCode":null,"errorMessage":"page.route('%s'): %w","messagePattern":"page\\.route\\('(.+?)'\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/browser/page_mapping.go","lineNumber":986,"sourceCode":"}\n\n// mapPageRoute maps the requested page.route event to the Sobek runtime.\nfunc mapPageRoute(vu moduleVU, p *common.Page) func(sobek.Value, sobek.Callable) (*sobek.Promise, error) {\n\treturn func(path sobek.Value, cb sobek.Callable) (*sobek.Promise, error) {\n\t\tctx := vu.Context()\n\n\t\tppath := parseStringOrRegex(path, false)\n\t\ttq := vu.get(ctx, p.TargetID())\n\n\t\troute := func(r *common.Route) error {\n\t\t\t_, err := queueTask(ctx, tq, func() (any, error) {\n\t\t\t\treturn cb(sobek.Undefined(), vu.Runtime().ToValue(mapRoute(vu, r)))\n\t\t\t})()\n\t\t\tif errors.Is(err, context.Canceled) {\n\t\t\t\treturn fmt.Errorf(\"page.route('%s'): iteration ended before route completed\", path)\n\t\t\t}\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"page.route('%s'): %w\", path, err)\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\treturn promise(vu, func() (any, error) {\n\t\t\treturn nil, p.Route(ppath, route, newRegExMatcher(ctx, vu, tq))\n\t\t}), nil\n\t}\n}\n\nfunc mapWaitForURL(rt *sobek.Runtime, vu moduleVU, target interface {\n\tTimeout() time.Duration\n\tWaitForURL(urlPattern string, opts *common.FrameWaitForURLOptions, rm common.RegExMatcher) error\n}, url sobek.Value, opts sobek.Value,\n) (*sobek.Promise, error) {\n\tif k6common.IsNullish(url) {\n\t\treturn nil, errors.New(\"missing required argument 'url'\")\n\t}","sourceCodeStart":968,"sourceCodeEnd":1004,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/browser/browser/page_mapping.go#L968-L1004","documentation":"General wrapper for failures inside a page.route(pattern, handler) interception: the JavaScript handler callback raised an error (anything other than context cancellation, which gets its own message). The error from executing the queued handler task is wrapped with the route pattern for context.","triggerScenarios":"An exception thrown inside the route handler (TypeError on the mapped Route object, calling a non-existent method); handler code referencing out-of-scope variables that became undefined; route.fulfill() given a malformed body type causing a JS-level throw.","commonSituations":"Handlers assuming Playwright's route.request() chaining; mocking responses where the body/headers construction throws; handlers that forget to settle the route and also throw on later cleanup; typos in route.continue()/fulfill/abort names.","solutions":["Wrap the handler body in try/catch and call route.abort() or route.continue() in the catch branch so the browser request never hangs.","Verify every method used exists on the mapped k6 Route object (fulfill, abort, continue, request).","Log the error inside the handler to identify which intercepted URL fails.","Narrow the route pattern so only intended requests invoke the handler."],"exampleFix":"// before\nawait page.route('**/api/config', route => route.fulfill({ body: JSON.stringify(cfg.payload) }));\n\n// after\nawait page.route('**/api/config', async route => {\n  try { await route.fulfill({ body: JSON.stringify(cfg.payload) }); }\n  catch (e) { await route.continue(); }\n});","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"await page.route('**/api/config', async (route) => {\n  try {\n    await route.fulfill({ status: 200, body: JSON.stringify(payload) });\n  } catch (e) {\n    console.log('route handler error, passing through:', e.message);\n    await route.continue();\n  }\n});","preventionTips":["Always provide a catch branch that calls route.continue() or route.abort() so the browser never hangs.","Pre-build response bodies before registering the route; keep the handler synchronous where possible.","Narrow route patterns so handlers only run for requests you actually mock."],"tags":["k6","browser","page","route","interception","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"}