{"record":{"id":"d91fbb33a2cd22fb","repo":"Hmbown/CodeWhale","slug":"compactruntimeerror-response-status-result","errorCode":null,"errorMessage":"${compactRuntimeError(response.status, result)}","messagePattern":"\\$\\{compactRuntimeError\\(response\\.status, result\\)\\}","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"integrations/weixin-bridge/src/index.mjs","lineNumber":208,"sourceCode":"  };\n}\n\nasync function readJsonSafe(response) {\n  try {\n    return await response.json();\n  } catch {\n    return null;\n  }\n}\n\nasync function runtimeJson(subPath, { method = \"GET\", body = null, auth = true } = {}) {\n  const url = `${config.runtimeUrl}${subPath}`;\n  const options = { method, headers: auth ? authHeaders() : {} };\n  if (body) options.body = JSON.stringify(body);\n  const response = await fetch(url, options);\n  const result = await readJsonSafe(response);\n  if (!response.ok) {\n    throw new Error(compactRuntimeError(response.status, result));\n  }\n  return result;\n}\n\nasync function* readSse(response) {\n  let buffer = \"\";\n  for await (const chunk of response.body) {\n    buffer += new TextDecoder().decode(chunk, { stream: true });\n    const lines = buffer.split(\"\\n\");\n    buffer = lines.pop() || \"\";\n    for (const line of lines) {\n      const trimmed = line.trim();\n      if (!trimmed) continue;\n      if (trimmed.startsWith(\"data:\")) {\n        yield { data: trimmed.slice(5).trim() };\n      } else if (trimmed.startsWith(\"event:\")) {\n        yield { event: trimmed.slice(6).trim() };\n      } else if (trimmed.startsWith(\"id:\")) {","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/integrations/weixin-bridge/src/index.mjs#L190-L226","documentation":"runtimeJson is the Weixin bridge's general-purpose JSON fetch helper for the runtime API: it builds the request, reads the JSON body via readJsonSafe, and throws compactRuntimeError(response.status, result) whenever the HTTP status is not OK. Any non-2xx runtime response on any method/subPath funnels through this single error, carrying the status and server-provided error details.","triggerScenarios":"Any runtimeJson call (GET/POST to config.runtimeUrl + subPath) where response.ok is false — invalid auth for protected endpoints, wrong subPath yielding 404, or a 5xx from the runtime process.","commonSituations":"Misconfigured runtimeUrl base, stale auth token after a runtime restart, calling an endpoint that does not exist in the deployed runtime version, or runtime returning 500 on a malformed request body.","solutions":["Parse the status/message: fix credentials (401/403), fix the URL/subPath (404), or fix the request payload (400).","Check runtime server logs for 5xx responses and restart/repair the runtime service.","Re-authenticate so authHeaders() carries a fresh valid token."],"exampleFix":"// before\nruntimeJson(\"POST\", \"/turns\", body) // -> Error: 401 ...\n// after: refresh token before the call\nconst token = await refreshRuntimeToken();\nruntimeJson(\"POST\", \"/turns\", body, token);","handlingStrategy":"try-catch","validationCode":"// Probe the runtime before issuing real calls:\nconst res = await fetch(`${config.runtimeUrl}/health`);\nif (!res.ok) throw new Error(`Runtime not ready: HTTP ${res.status}`);","typeGuard":null,"tryCatchPattern":"try {\n  const result = await runtimeJson(\"POST\", \"/turns\", body);\n} catch (err) {\n  if (/^40[13]/.test(err.message)) {\n    await reauthenticate();\n  } else if (/^404/.test(err.message)) {\n    console.error(\"Endpoint not found: check runtimeUrl and runtime version\");\n  } else if (/^5\\d\\d/.test(err.message)) {\n    await retryWithBackoff();\n  } else {\n    throw err;\n  }\n}","preventionTips":["Centralize all runtime calls through runtimeJson (already the case) so error handling is uniform.","Keep tokens fresh relative to runtime restarts; re-auth on any 401.","Validate request bodies client-side to avoid 400s.","Version-check the runtime on startup to catch endpoint drift early."],"tags":["http","network","runtime","api"],"backgroundTag":"http-error-response","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T06:17:15.046Z"}