{"record":{"id":"8b3c4828da649aae","repo":"grafana/k6","slug":"s","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/execution_context.go","lineNumber":224,"sourceCode":"\n\tvar (\n\t\tremoteObject     *runtime.RemoteObject\n\t\texceptionDetails *runtime.ExceptionDetails\n\t\terr              error\n\t)\n\tif remoteObject, exceptionDetails, err = action.Do(cdp.WithExecutor(apiCtx, e.session)); err != nil {\n\t\tvar cdpe *cdproto.Error\n\t\tif errors.As(err, &cdpe) && cdpe.Code == devToolsServerErrorCode {\n\t\t\t// By creating a new error instead of reusing it, we're removing the\n\t\t\t// chromium specific error code.\n\t\t\treturn nil, errors.New(cdpe.Message)\n\t\t}\n\n\t\te.logger.Debugf(\"ExecutionContext:eval\", \"Unexpected DevTools server error: %v\", err)\n\t\treturn nil, err\n\t}\n\tif exceptionDetails != nil {\n\t\treturn nil, fmt.Errorf(\"%s\", parseExceptionDetails(exceptionDetails))\n\t}\n\tvar res any\n\tif remoteObject == nil {\n\t\te.logger.Debugf(\n\t\t\t\"ExecutionContext:eval\",\n\t\t\t\"sid:%s stid:%s fid:%s ectxid:%d furl:%q remoteObject is nil\",\n\t\t\te.sid, e.stid, e.fid, e.id, e.furl)\n\t\treturn res, nil\n\t}\n\n\tif opts.returnByValue {\n\t\tres, err = valueFromRemoteObject(apiCtx, remoteObject)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\n\t\t\t\t\"extracting value from remote object with ID %s: %w\",\n\t\t\t\tremoteObject.ObjectID, err)\n\t\t}\n\t} else if remoteObject.ObjectID != \"\" {","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/execution_context.go#L206-L242","documentation":"The JavaScript that k6 evaluated inside the browser threw an exception. CDP returned exceptionDetails and k6 formats them (script, line, exception text) into this error. It is not a k6 failure: it is the page-side script failing — a syntax error in an evaluated string, a runtime error (undefined variable, null dereference), or an API call failing inside the page.","triggerScenarios":"page.evaluate('document.querySelector(\"#missing\").click()') where the selector misses (TypeError on null); referencing variables that do not exist in the page context; syntax errors or unbalanced quotes in string-based evaluate calls; expected page errors such as JSON.parse of a failed fetch.","commonSituations":"Assuming selectors or globals exist without waiting; evaluating code copied from DevTools that depends on page globals; testing failure paths where the page itself throws; evaluating before the page's own scripts initialized.","solutions":["Read the message: it contains the page exception text and location","Guard inside the evaluated script (null checks) or wait for the element first","Quote strings carefully when passing code as a string; prefer function form","If the page legitimately throws, catch it inside the evaluated code and return the error instead"],"exampleFix":"// before\nawait page.evaluate('document.querySelector(\"#x\").click()');\n\n// after\nawait page.click('#x'); // auto-waits for the element, or:\nawait page.evaluate('const el = document.querySelector(\"#x\"); if (el) el.click();');","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await page.evaluate(fn);\n} catch (e) {\n  // e.message carries the page-side exception; decide whether it is expected\n  if (!/MyExpectedError/.test(e.message)) throw e;\n}","preventionTips":["Wait for elements/state before evaluating code that uses them","Prefer evaluate with a function over string blobs","Make page scripts defensive (null checks) when selectors may be absent","Keep evaluate snippets small so failures are obvious"],"tags":["browser","evaluate","javascript","page-error"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}