{"id":"6f43070ab2697a6c","repo":"sindresorhus/got","slug":"err-body-parse-failure","errorCode":"ERR_BODY_PARSE_FAILURE","errorMessage":"Unknown body type '${responseType as string}'","messagePattern":"Unknown body type '(.+?)'","errorType":"exception","errorClass":"ParseError","httpStatus":null,"severity":"error","filePath":"source/core/response.ts","lineNumber":187,"sourceCode":"\t\t}\n\n\t\tif (responseType === 'json') {\n\t\t\tif (rawBody.length === 0) {\n\t\t\t\treturn '';\n\t\t\t}\n\n\t\t\tconst text = cachedDecodedBody ?? decodeUint8Array(rawBody, encoding);\n\t\t\treturn parseJson(text);\n\t\t}\n\n\t\tif (responseType === 'buffer') {\n\t\t\treturn rawBody;\n\t\t}\n\t} catch (error) {\n\t\tthrow new ParseError(error as Error, response);\n\t}\n\n\tthrow new ParseError({\n\t\tmessage: `Unknown body type '${responseType as string}'`,\n\t\tname: 'Error',\n\t}, response);\n};\n","sourceCodeStart":169,"sourceCodeEnd":192,"githubUrl":"https://github.com/sindresorhus/got/blob/e3924aa1e53a6ca3eb93a43618ce532442a89b40/source/core/response.ts#L169-L192","documentation":"From `parseBody` in response.ts:187: Got supports only `text`, `json`, and `buffer` response types. If `responseType` is anything else (misspelled, an unknown string, or a value leaked from a custom option), the function falls through all branches and throws a `ParseError` with code `ERR_BODY_PARSE_FAILURE` and the offending type in the message. The error carries the `response` so callers can inspect it.","triggerScenarios":"Calling Got with `responseType: 'stream'` (not supported — that's the `isStream` option), `responseType: 'html'`, `responseType: undefined` after a bad merge, or a typo like `responseType: 'json '`. The throw happens during body decoding after the response is received.","commonSituations":"Confusing `responseType` (parse format) with `isStream` (transport mode); a shared defaults object that gets `responseType` overwritten by a typo; dynamically-built option objects where an undefined sneaks in; version confusion where docs mention an unavailable type.","solutions":["Set `responseType` to one of `'text' | 'json' | 'buffer'` (the only valid values).","For streaming responses use `got.stream(...)` or `{isStream: true}`, not `responseType: 'stream'`.","Type the option as `ResponseOptionType` (the library's union) so TypeScript rejects invalid values at compile time.","Audit shared defaults/extends for stray `responseType` overrides."],"exampleFix":"// before\nconst {body} = await got(url, {responseType: 'stream'});\n\n// after\nimport got from 'got';\nconst stream = got.stream(url);\n// or, for a buffered parse:\nconst {body} = await got(url, {responseType: 'json'});","handlingStrategy":"type-guard","validationCode":"const VALID = new Set(['text', 'json', 'buffer']);\nfunction safeResponseType(value) {\n  return VALID.has(value) ? value : undefined;\n}\nconst responseType = safeResponseType(myConfig.responseType);\nif (!responseType) throw new Error('invalid responseType');\nawait got(url, {responseType});","typeGuard":"const isResponseType = (v: unknown): v is 'text' | 'json' | 'buffer' =>\n  v === 'text' || v === 'json' || v === 'buffer';","tryCatchPattern":"try {\n  const {body} = await got(url, {responseType});\n} catch (error) {\n  if (error.code === 'ERR_BODY_PARSE_FAILURE' && /Unknown body type/.test(error.message)) {\n    // responseType was invalid; fix the option and retry\n  } else throw error;\n}","preventionTips":["Type the option as the library's `ResponseOptionType` union so invalid values fail at compile time.","Don't confuse `responseType` with `isStream` — use `got.stream()` for streams.","Centralize responseType selection in a typed helper.","Unit-test the option builder to ensure it only emits valid values."],"tags":["response","body-parsing","typescript","configuration"],"analyzedSha":"e3924aa1e53a6ca3eb93a43618ce532442a89b40","analyzedAt":"2026-08-03T19:22:24.770Z","schemaVersion":2}