From ddb0cc174d2a14c0948dcdaf9bae78620937c667 Mon Sep 17 00:00:00 2001 From: Rafael Martins Date: Mon, 6 Feb 2023 06:40:51 -0300 Subject: [PATCH] fix: `page.goto` error throwing on 40x/50x responses with an empty body (#9523) (#9577) Co-authored-by: Alex Rudenko --- packages/puppeteer-core/src/common/Frame.ts | 3 +++ test/src/navigation.spec.ts | 24 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 47ebec62e3c..4c387d6c656 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -354,6 +354,9 @@ export class Frame { referrerPolicy, }); ensureNewDocumentNavigation = !!response.loaderId; + if (response.errorText === 'net::ERR_HTTP_RESPONSE_CODE_FAILURE') { + return null; + } return response.errorText ? new Error(`${response.errorText} at ${url}`) : null; diff --git a/test/src/navigation.spec.ts b/test/src/navigation.spec.ts index fea8015c60b..91530da4c60 100644 --- a/test/src/navigation.spec.ts +++ b/test/src/navigation.spec.ts @@ -304,6 +304,30 @@ describe('navigation', function () { expect(response.ok()).toBe(false); expect(response.status()).toBe(404); }); + it('should not throw an error for a 404 response with an empty body', async () => { + const {page, server} = getTestState(); + + server.setRoute('/404-error', (_, res) => { + res.statusCode = 404; + res.end(); + }); + + const response = (await page.goto(server.PREFIX + '/404-error'))!; + expect(response.ok()).toBe(false); + expect(response.status()).toBe(404); + }); + it('should not throw an error for a 500 response with an empty body', async () => { + const {page, server} = getTestState(); + + server.setRoute('/500-error', (_, res) => { + res.statusCode = 500; + res.end(); + }); + + const response = (await page.goto(server.PREFIX + '/500-error'))!; + expect(response.ok()).toBe(false); + expect(response.status()).toBe(500); + }); it('should return last response in redirect chain', async () => { const {page, server} = getTestState();