{"record":{"id":"f7cace81caef40dc","repo":"grafana/k6","slug":"s-f7cace","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":895,"sourceCode":"func (f *Frame) Evaluate(pageFunc string, args ...any) (any, error) {\n\tf.log.Debugf(\"Frame:Evaluate\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\treturn f.EvaluateWithContext(f.ctx, pageFunc, args...)\n}\n\n// EvaluateGlobal will evaluate the given JS code in the global object.\nfunc (f *Frame) EvaluateGlobal(ctx context.Context, js string) error {\n\taction := runtime.Evaluate(js).WithAwaitPromise(true)\n\n\tvar (\n\t\texceptionDetails *runtime.ExceptionDetails\n\t\terr              error\n\t)\n\tif _, exceptionDetails, err = action.Do(cdp.WithExecutor(ctx, f.manager.session)); err != nil {\n\t\treturn fmt.Errorf(\"evaluating JS in global context: %w\", err)\n\t}\n\tif exceptionDetails != nil {\n\t\treturn fmt.Errorf(\"%s\", parseExceptionDetails(exceptionDetails))\n\t}\n\n\treturn nil\n}\n\n// EvaluateHandle will evaluate provided page function within an execution context.\nfunc (f *Frame) EvaluateHandle(pageFunc string, args ...any) (handle JSHandleAPI, _ error) {\n\tf.log.Debugf(\"Frame:EvaluateHandle\", \"fid:%s furl:%q\", f.ID(), f.URL())\n\n\tevalHandle := func() (JSHandleAPI, error) {\n\t\tf.executionContextMu.RLock()\n\t\tdefer f.executionContextMu.RUnlock()\n\n\t\tec := f.executionContexts[mainWorld]\n\t\tif ec == nil {\n\t\t\treturn nil, fmt.Errorf(\"evaluating handle for frame: execution context %q not found\", mainWorld)\n\t\t}\n\t\treturn ec.EvalHandle(f.ctx, pageFunc, args...) //nolint:wrapcheck","sourceCodeStart":877,"sourceCodeEnd":913,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L877-L913","documentation":"When Frame.EvaluateGlobal()'s CDP call succeeds but returns exceptionDetails, k6 formats them via parseExceptionDetails and returns that text as the error. In other words: the transport worked, but the JS you passed threw an uncaught exception in the page's global context.","triggerScenarios":"The evaluated code references undefined globals, has a syntax error, or triggers a runtime error (null property access, JSON.parse of invalid data). With awaitPromise, a rejected promise also surfaces here.","commonSituations":"Stubbing window functions with a typo; evaluating code that assumes a library (jQuery) already loaded; awaiting a promise that rejects; version skew where a global API changed in the target page.","solutions":["Paste the exact JS string into DevTools console on the same page and fix the exception it reports.","Guard inside the evaluated code: wrap in try/catch and return an error value instead of throwing.","Ensure the code runs after the scripts/globals it depends on have loaded."],"exampleFix":"// before\npage.evaluateGlobal('JSON.parse(window.__cfg).urls[0]'); // throws if __cfg unset\n\n// after\npage.evaluateGlobal(`\n  try {\n    JSON.parse(window.__cfg || '{}').urls[0] || null;\n  } catch (e) {\n    null;\n  }\n`);","handlingStrategy":"try-catch","validationCode":"// verify the snippet runs cleanly in-page before using it:\nawait frame.evaluate(`(() => { try { ${js}; return 'ok'; } catch (e) { return 'err: ' + e.message; } })()`);","typeGuard":null,"tryCatchPattern":"try {\n  page.evaluateGlobal(js);\n} catch (e) {\n  console.warn('global JS threw in page:', e.message);\n}","preventionTips":["Wrap global snippets in try/catch inside the page.","Confirm referenced globals exist on the loaded page.","Test snippets in DevTools on the exact page state."],"tags":["browser","evaluate","javascript-exception","cdp","global-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}