{"record":{"id":"abb918227b541d4b","repo":"anomalyco/sst","slug":"failed-to-parse-json-response-text","errorCode":null,"errorMessage":"Failed to parse JSON response: ${text}","messagePattern":"Failed to parse JSON response: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"sdk/js/src/vector/index.ts","lineNumber":317,"sourceCode":"    const c = await client();\n    const endpoint = `https://lambda.${process.env.AWS_REGION}.amazonaws.com/2015-03-31`;\n    const response = await c.fetch(\n      `${endpoint}/functions/${functionName}/invocations`,\n      {\n        method: \"POST\",\n        headers: { Accept: \"application/json\" },\n        body,\n      }\n    );\n\n    // success\n    if (response.status === 200 || response.status === 201) {\n      if (response.headers.get(\"content-length\") === \"0\") return undefined as T;\n      const text = await response.text();\n      try {\n        return JSON.parse(text);\n      } catch (e) {\n        throw new Error(`Failed to parse JSON response: ${text}`);\n      }\n    }\n\n    // error\n    const error = new Error();\n    const text = await response.text();\n    try {\n      const json = JSON.parse(text);\n      error.name = json.Error?.Code;\n      error.message = json.Error?.Message ?? json.message ?? text;\n    } catch (e) {\n      error.message = text;\n    }\n    error.name = error.name ?? response.headers.get(\"x-amzn-ErrorType\");\n    // @ts-expect-error\n    error.requestID = response.headers.get(\"x-amzn-RequestId\");\n    // @ts-expect-error\n    error.statusCode = response.status;","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/sdk/js/src/vector/index.ts#L299-L335","documentation":"The Vector client's invokeFunction parses the HTTP response body as JSON when status is 200/201 and content-length is non-zero. If JSON.parse fails, it throws this error embedding the raw response text. It means the vectorized function returned a 2xx response whose body is not valid JSON.","triggerScenarios":"Calling a vector function (via VectorClient) whose handler returns a non-JSON body (plain text, HTML, empty-ish string, truncated response) with status 200/201 and a non-zero content-length.","commonSituations":"The function handler returns a raw string instead of an object; a proxy/gateway returns an HTML error page with status 200; response body is truncated by a size limit or interrupted stream; content-type is text/plain.","solutions":["Check the embedded text in the error message to see what the function actually returned.","Fix the vector function handler to return a JSON-serializable object (not a raw string or undefined with a non-zero body).","Verify any gateway/proxy in front of the function isn't rewriting the body (e.g. HTML interstitial with 200).","Confirm the response isn't being truncated (response size limits, streaming issues) — compare content-length with received bytes.","Set the response Content-Type to application/json and test the endpoint directly with curl."],"exampleFix":"// before (vector function handler)\nexport const handler = async () => {\n  return \"done\"; // non-JSON body\n};\n\n// after\nexport const handler = async () => {\n  return { status: \"done\" }; // JSON-serializable object\n};","handlingStrategy":"try-catch","validationCode":"// pre-check the endpoint returns JSON before relying on the client\nconst res = await fetch(fnUrl);\nconst ct = res.headers.get(\"content-type\") ?? \"\";\nif (!ct.includes(\"application/json\")) throw new Error(`Vector function returns non-JSON: ${ct}`);","typeGuard":"function looksLikeJson(text: string): boolean {\n  try { JSON.parse(text); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const result = await client.invoke(payload);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"Failed to parse JSON response:\")) {\n    console.error(\"Raw body was:\", e.message.slice(\"Failed to parse JSON response:\".length));\n  }\n  throw e;\n}","preventionTips":["Make vector function handlers return JSON-serializable objects, never raw strings.","Set Content-Type: application/json in the function response.","Test the function endpoint directly with curl to inspect the raw body.","Watch for proxies/gateways substituting HTML bodies with a 200 status."],"tags":["json","http","vector","serialization"],"backgroundTag":"json-parse-error","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}