From 57e14864d84c84a7d35a3a2d8cac096eec7943ac Mon Sep 17 00:00:00 2001 From: archvlad Date: Sun, 3 Mar 2024 22:53:25 +0300 Subject: [PATCH 1/3] catch error in fall back to utf8 --- source/as-promise/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index eee8903da..35d2bff63 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -7,7 +7,7 @@ import { type RequestError, } from '../core/errors.js'; import Request from '../core/index.js'; -import {parseBody, isResponseOk, type Response} from '../core/response.js'; +import {parseBody, isResponseOk, type Response, ParseError} from '../core/response.js'; import proxyEvents from '../core/utils/proxy-events.js'; import type Options from '../core/options.js'; import {CancelError, type CancelableRequest} from './types.js'; @@ -62,7 +62,12 @@ export default function asPromise(firstRequest?: Request): CancelableRequest< response.body = parseBody(response, options.responseType, options.parseJson, options.encoding); } catch (error: any) { // Fall back to `utf8` - response.body = response.rawBody.toString(); + try { + response.body = response.rawBody.toString(); + } catch (error) { + request._beforeError(new ParseError(error as Error, response)); + return; + } if (isResponseOk(response)) { request._beforeError(error); From 4a219ef3fa784a8df3194666793c9dad8772b849 Mon Sep 17 00:00:00 2001 From: archvlad Date: Tue, 5 Mar 2024 21:58:40 +0300 Subject: [PATCH 2/3] added test --- test/error.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/error.ts b/test/error.ts index 273e08308..36c25fdf6 100644 --- a/test/error.ts +++ b/test/error.ts @@ -343,6 +343,23 @@ test('no uncaught parse errors #2', async t => { await close(); }); +test('no uncaught parse errors on fall back to utf8', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + const buffer = Buffer.alloc(536_870_912, 'A'); + response.statusCode = 200; + response.end(buffer); + }); + + await t.throwsAsync(got({ + timeout: { + request: 60_000, + }, + }), { + instanceOf: RequestError, + code: 'ERR_BODY_PARSE_FAILURE', + }); +}); + // Fails randomly on Node 10: // Blocked by https://github.com/istanbuljs/nyc/issues/619 // eslint-disable-next-line ava/no-skip-test From 93a5c9b3775ff6d1af7be4555f14bac70ed99978 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 6 Mar 2024 13:00:47 +0700 Subject: [PATCH 3/3] Update error.ts --- test/error.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/error.ts b/test/error.ts index 36c25fdf6..1360733c2 100644 --- a/test/error.ts +++ b/test/error.ts @@ -343,7 +343,7 @@ test('no uncaught parse errors #2', async t => { await close(); }); -test('no uncaught parse errors on fall back to utf8', withServer, async (t, server, got) => { +test('no uncaught parse errors on fallback to utf8', withServer, async (t, server, got) => { server.get('/', (_request, response) => { const buffer = Buffer.alloc(536_870_912, 'A'); response.statusCode = 200;