{"record":{"id":"5a44098273cee1e0","repo":"jackwener/OpenCLI","slug":"failed-to-fetch-cdp-targets-http-response-statu","errorCode":null,"errorMessage":"Failed to fetch CDP targets: HTTP ${response.status}","messagePattern":"Failed to fetch CDP targets: HTTP (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/trae-cn/targets.js","lineNumber":35,"sourceCode":"}\n\nfunction targetValue(target) {\n  const title = String(target?.title || '').trim();\n  const workspace = title.match(/—\\s*(.+)$/)?.[1]?.trim();\n  return workspace || title || String(target?.url || '').trim();\n}\n\nfunction matchesTarget(target, wanted) {\n  if (!wanted) return false;\n  const needle = String(wanted).toLowerCase();\n  const haystack = `${target.title || ''} ${target.url || ''}`.toLowerCase();\n  return haystack.includes(needle);\n}\n\nasync function fetchTargets(endpoint) {\n  const response = await fetch(`${endpoint}/json`, { signal: AbortSignal.timeout(5000) });\n  if (!response.ok) {\n    throw new Error(`Failed to fetch CDP targets: HTTP ${response.status}`);\n  }\n  const targets = await response.json();\n  return Array.isArray(targets) ? targets : [];\n}\n\nasync function probeTargetActivity(target, maxChars) {\n  if (!target.webSocketDebuggerUrl) return null;\n  const bridge = new CDPBridge();\n  try {\n    const page = await bridge.connect({ cdpEndpoint: target.webSocketDebuggerUrl, timeout: 3, surface: 'adapter' });\n    return await page.evaluate(activityScript(maxChars));\n  } catch {\n    return null;\n  } finally {\n    await bridge.close().catch(() => {});\n  }\n}\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/trae-cn/targets.js#L17-L53","documentation":"fetchTargets in targets.js throws a plain Error when the HTTP GET to `${endpoint}/json` (the Chrome DevTools Protocol target list) returns a non-2xx status within its 5-second AbortSignal timeout. The status code is embedded in the message to indicate why the CDP endpoint refused or failed the request.","triggerScenarios":"Hitting an endpoint that is not a CDP debugger (wrong port serving a different app), a proxy returning 403/502, or the DevTools server rejecting /json after a Chrome/Electron version change that disabled the targets endpoint.","commonSituations":"OPENCLI_CDP_ENDPOINT pointing at the app's HTTP port instead of the remote-debugging port; corporate proxy intercepting localhost calls; Trae CN relaunched without --remote-debugging-port so another service owns the port.","solutions":["Verify the port with `opencli trae-cn targets` against the actual remote-debugging port; confirm with `curl http://127.0.0.1:<port>/json`.","Ensure Trae CN is running with remote debugging enabled (correct --remote-debugging-port).","Fix OPENCLI_CDP_ENDPOINT if it points at the wrong port or protocol (http vs https).","Bypass any HTTP proxy for localhost (NO_PROXY=127.0.0.1,localhost)."],"exampleFix":"// before\nexport OPENCLI_CDP_ENDPOINT=http://127.0.0.1:8080  // app port, not CDP\n// after\ncurl http://127.0.0.1:39240/json   # confirm CDP responds\nexport OPENCLI_CDP_ENDPOINT=http://127.0.0.1:39240","handlingStrategy":"retry","validationCode":"// Pre-flight: confirm the CDP /json endpoint is reachable before calling fetchTargets\nasync function assertCdpHealthy(endpoint) {\n  const res = await fetch(`${endpoint.replace(/\\/$/, '')}/json`, { signal: AbortSignal.timeout(5000) });\n  if (!res.ok) throw new Error(`CDP endpoint ${endpoint} unhealthy: HTTP ${res.status}`);\n  const t = await res.json();\n  if (!Array.isArray(t)) throw new Error('Endpoint did not return a CDP target list');\n}","typeGuard":"function isCdpTargetList(v) {\n  return Array.isArray(v) && v.every(t => t && typeof t === 'object' && 'id' in t && 'webSocketDebuggerUrl' in t);\n}","tryCatchPattern":"async function fetchTargetsWithRetry(endpoint, attempts = 3) {\n  for (let i = 0; i < attempts; i++) {\n    try {\n      return await fetchTargets(endpoint);\n    } catch (e) {\n      if (i === attempts - 1) throw e;\n      if (!/Failed to fetch CDP targets|HTTP 5/.test(e.message)) throw e;\n      await new Promise(r => setTimeout(r, 1000 * (i + 1)));\n    }\n  }\n}","preventionTips":["Point OPENCLI_CDP_ENDPOINT at the remote-debugging port, not the app's HTTP port.","Confirm with `curl http://127.0.0.1:<port>/json` before scripting.","Set NO_PROXY=127.0.0.1,localhost if a proxy intercepts requests.","Re-launch Trae CN with remote debugging enabled after updates."],"tags":["network","cdp","http","debugger-protocol"],"backgroundTag":"cdp-targets-fetch-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}