{"id":"c6dab36003e8480d","repo":"node-fetch/node-fetch","slug":"system","errorCode":"system","errorMessage":"Could not create Buffer from response body for ${data.url}: ${error.message}","messagePattern":"Could not create Buffer from response body for (.+?): (.+?)","errorType":"exception","errorClass":"FetchError","httpStatus":null,"severity":"error","filePath":"src/body.js","lineNumber":246,"sourceCode":"\t\t\t}\n\n\t\t\taccumBytes += chunk.length;\n\t\t\taccum.push(chunk);\n\t\t}\n\t} catch (error) {\n\t\tconst error_ = error instanceof FetchBaseError ? error : new FetchError(`Invalid response body while trying to fetch ${data.url}: ${error.message}`, 'system', error);\n\t\tthrow error_;\n\t}\n\n\tif (body.readableEnded === true || body._readableState.ended === true) {\n\t\ttry {\n\t\t\tif (accum.every(c => typeof c === 'string')) {\n\t\t\t\treturn Buffer.from(accum.join(''));\n\t\t\t}\n\n\t\t\treturn Buffer.concat(accum, accumBytes);\n\t\t} catch (error) {\n\t\t\tthrow new FetchError(`Could not create Buffer from response body for ${data.url}: ${error.message}`, 'system', error);\n\t\t}\n\t} else {\n\t\tthrow new FetchError(`Premature close of server response while trying to fetch ${data.url}`);\n\t}\n}\n\n/**\n * Clone body given Res/Req instance\n *\n * @param   Mixed   instance       Response or Request instance\n * @param   String  highWaterMark  highWaterMark for both PassThrough body streams\n * @return  Mixed\n */\nexport const clone = (instance, highWaterMark) => {\n\tlet p1;\n\tlet p2;\n\tlet {body} = instance[INTERNALS];\n","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/node-fetch/node-fetch/blob/8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f/src/body.js#L228-L264","documentation":"A FetchError with code 'system' thrown at src/body.js:246 when Buffer.concat (or Buffer.from on string chunks) fails while materializing the accumulated response bytes. node-fetch gathers stream chunks in an array and concatenates them at the end; if those chunks are not valid Buffer-compatible values the concat throws and is rewrapped as a FetchError so callers can distinguish transport-level corruption from application errors.","triggerScenarios":"A response stream emitting chunks that are not Buffers/Uint8Arrays (custom push of strings or objects on a hand-rolled server); memory exhaustion where Buffer.concat cannot allocate; a corrupted stream where the upstream pushes unexpected chunk types after a transform.","commonSituations":"Proxies or test servers that push non-Buffer data into the readable; very large responses hitting process memory limits; interop with streams that default to objectMode; broken compression pipelines feeding garbage chunks.","solutions":["Inspect the underlying cause on error.cause — it carries the original message","Verify the upstream server is sending well-formed bytes (curl the endpoint and compare)","If you control the source stream, ensure it pushes Buffers or Uint8Arrays only","For very large responses, stream instead of buffering (consume response.body directly)"],"exampleFix":"// before\nconst buf = await response.buffer();\n\n// after - stream the body instead of buffering it whole\nfor await (const chunk of response.body) {\n  process.stdout.write(chunk);\n}","handlingStrategy":"try-catch","validationCode":"// Limit response size before buffering to avoid memory issues\nconst MAX = 50 * 1024 * 1024; // 50MB ceiling\nif (response.headers.get('content-length') && Number(response.headers.get('content-length')) > MAX) {\n  throw new Error('Response too large to buffer; stream it instead');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const buf = await response.arrayBuffer();\n} catch (e) {\n  if (e?.code === 'system' && /Could not create Buffer/.test(e.message)) {\n    // upstream is emitting bad chunks - re-fetch or switch to streaming\n  }\n  throw e;\n}","preventionTips":["Stream response.body directly for large payloads instead of buffering","Validate the upstream server emits Buffers/Uint8Arrays","Set a sensible response size cap in your fetch wrapper","Log e.cause to capture the original Buffer.concat error"],"tags":["body","buffer","stream","memory","system"],"analyzedSha":"8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f","analyzedAt":"2026-08-03T19:14:11.207Z","schemaVersion":2}