{"record":{"id":"09361bf4edc22a90","repo":"Hmbown/CodeWhale","slug":"compactruntimeerror-response-status-body","errorCode":null,"errorMessage":"${compactRuntimeError(response.status, body)}","messagePattern":"\\$\\{compactRuntimeError\\(response\\.status, body\\)\\}","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"integrations/bridge-core/src/lib.mjs","lineNumber":465,"sourceCode":"}\n\nexport function createRuntimeClient({ runtimeUrl, runtimeToken }) {\n  function authHeaders() {\n    return { authorization: `Bearer ${runtimeToken}` };\n  }\n\n  async function runtimeJson(route, options = {}) {\n    const response = await fetch(`${runtimeUrl}${route}`, {\n      method: options.method || \"GET\",\n      headers: {\n        ...(options.auth === false ? {} : authHeaders()),\n        ...(options.body ? { \"content-type\": \"application/json\" } : {})\n      },\n      body: options.body ? JSON.stringify(options.body) : undefined\n    });\n    const body = await readJsonSafe(response);\n    if (!response.ok) {\n      throw new Error(compactRuntimeError(response.status, body));\n    }\n    return body;\n  }\n\n  return { runtimeJson, authHeaders };\n}\n\nexport function compactRuntimeError(status, body) {\n  const message =\n    body?.error?.message ||\n    body?.message ||\n    (typeof body === \"string\" ? body : JSON.stringify(body));\n  return `Runtime API request failed (${status}): ${message}`;\n}\n\nexport function latestRunningTurn(detail) {\n  const turns = Array.isArray(detail?.turns) ? detail.turns : [];\n  for (let index = turns.length - 1; index >= 0; index -= 1) {","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/integrations/bridge-core/src/lib.mjs#L447-L483","documentation":"createRuntimeClient's runtimeJson helper throws whenever the Runtime HTTP response is not ok, formatting the status plus any JSON error body through compactRuntimeError. This is the bridge-core client's unified surface for all Runtime API failures.","triggerScenarios":"Any runtimeJson() call (or bridge using createRuntimeClient) where the Runtime responds 4xx/5xx — auth rejection, unknown route, invalid request body, or Runtime crash handling.","commonSituations":"Bridge configured against a wrong RUNTIME_URL/port; Runtime not running (connection error surfaces differently) or returning 500 from a bad turn request; stale auth headers after a Runtime restart.","solutions":["Read the compacted status/body in the thrown message to identify the specific HTTP failure, and fix the corresponding request (auth, body, path).","Verify the Runtime is running and the bridge's runtime URL/port env vars point at it (curl the endpoint manually).","Re-authenticate or restart the Runtime if the status is 401/403 (stale session or rotated credentials)."],"exampleFix":"// before: bridge points at wrong port\nRUNTIME_URL=http://127.0.0.1:3000\n// after: match the port codewhale web prints\nRUNTIME_URL=http://127.0.0.1:8787","handlingStrategy":"try-catch","validationCode":"const probe = await fetch(runtimeUrl + '/v1/providers');\nif (!probe.ok) throw new Error(`runtime not reachable/healthy: ${probe.status}`);","typeGuard":"function isRuntimeHttpError(err) {\n  return err instanceof Error && /\\b[45]\\d\\d\\b/.test(err.message);\n}","tryCatchPattern":"try {\n  body = await runtimeJson('/v1/turns', { method: 'POST', body });\n} catch (e) {\n  console.error('[bridge] runtime call failed:', e.message); // includes status + body\n  return fallbackReply('The assistant backend is unavailable.');\n}","preventionTips":["Health-check the Runtime URL at bridge startup and fail fast with a clear message.","Keep RUNTIME_URL/port env vars in one sourced config shared by all bridges.","Re-run auth setup whenever the Runtime restarts or rotates sessions."],"tags":["http","bridge","runtime-api","error-handling"],"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-23T08:17:48.524Z"}