{"record":{"id":"d41975a1de104e76","repo":"jackwener/OpenCLI","slug":"errmsg-d41975","errorCode":null,"errorMessage":"${errMsg}","messagePattern":"\\$\\{errMsg\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"extension/src/cdp.ts","lineNumber":254,"sourceCode":"  // attach retries; a debugger error mid-evaluate invalidates the attach\n  // cache so the next attempt re-attaches.\n  try {\n    await ensureAttached(tabId, aggressiveRetry);\n\n    const result = await sendDebuggerCommand({ tabId }, 'Runtime.evaluate', {\n      expression,\n      returnByValue: true,\n      awaitPromise: true,\n    }, timeoutMs) as {\n      result?: { type: string; value?: unknown; description?: string; subtype?: string };\n      exceptionDetails?: { exception?: { description?: string }; text?: string };\n    };\n\n    if (result.exceptionDetails) {\n      const errMsg = result.exceptionDetails.exception?.description\n        || result.exceptionDetails.text\n        || 'Eval error';\n      throw new Error(errMsg);\n    }\n\n    return result.result?.value;\n  } catch (e) {\n    const msg = e instanceof Error ? e.message : String(e);\n    if (msg.includes('Detached') || msg.includes('Debugger is not attached') || msg.includes('Target closed')) {\n      attached.delete(tabId); // Force re-attach on the next command\n    }\n    throw e;\n  }\n}\n\nexport const evaluateAsync = evaluate;\n\n/**\n * Capture a screenshot via CDP Page.captureScreenshot.\n * Returns base64-encoded image data.\n */","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/extension/src/cdp.ts#L236-L272","documentation":"cdp.evaluate ran a Runtime.evaluate expression that produced an uncaught JS exception in the page; the library rethrows the exception's description (or the CDP text) verbatim to the caller. This is the page's own error, not a transport failure — detached/target-closed messages get special handling, everything else is surfaced as-is.","triggerScenarios":"Runtime.evaluate returns result.exceptionDetails because the evaluated expression threw — e.g. calling a method on undefined, a SyntaxError in the expression, or an unhandled promise rejection with returnByValue/awaitPromise enabled.","commonSituations":"Selector returned null and the expression dereferences it; page CSP or bundles change global names; typos in evaluated JS; evaluating before the page finished loading so expected globals don't exist yet.","solutions":["Fix the evaluated JavaScript — null-check objects before dereferencing","Wrap the expression in try/catch inside the page and return a diagnostic value","Ensure the page is fully loaded before evaluating (wait for load/DOMContentLoaded)","Validate that globals/APIs the expression relies on still exist in the current page bundle"],"exampleFix":"// before\nawait cdp.evaluate(tabId, 'document.querySelector(\".a\").textContent')\n// after\nawait cdp.evaluate(tabId, 'document.querySelector(\".a\")?.textContent ?? null')","handlingStrategy":"try-catch","validationCode":"const probe = await cdp.evaluate(tabId, `(typeof document !== 'undefined')`);\nif (probe !== true) throw new Error('page not ready for evaluation');","typeGuard":"function isPageScriptError(e: unknown): e is Error & { pageException: true } {\n  const m = e instanceof Error ? e.message : String(e);\n  return !/Detached|not attached|Target closed/.test(m);\n}","tryCatchPattern":"try {\n  return await cdp.evaluate(tabId, expr);\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e);\n  if (/Detached|not attached|Target closed/.test(msg)) return reattachAndRetry();\n  console.error('page threw:', msg);   // genuine page exception\n  throw e;\n}","preventionTips":["Write defensive JS in evaluated expressions (optional chaining, null checks)","Wait for page load before evaluate","Wrap complex expressions in try/catch inside the page","Re-verify global names after page bundle updates"],"tags":["cdp","runtime-evaluate","javascript","page-exception"],"backgroundTag":"page-evaluation-exception","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}