From 398ca2a0190d6c86d908a71c887260bf13ef5e2b Mon Sep 17 00:00:00 2001 From: marekharanczyk <48673767+marekharanczyk@users.noreply.github.com> Date: Mon, 5 Feb 2024 08:36:51 +0100 Subject: [PATCH] fix: make sure invalid URL loads promises are fulfilled. (#41194) * fix: make sure invalid URL loads promisses are fulfilled. * fixup! fix: make sure invalid URL loads promisses are fulfilled. --- lib/browser/api/web-contents.ts | 13 ++++++++----- spec/api-web-contents-spec.ts | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/browser/api/web-contents.ts b/lib/browser/api/web-contents.ts index 7fcd5dec3f..070db86390 100644 --- a/lib/browser/api/web-contents.ts +++ b/lib/browser/api/web-contents.ts @@ -366,11 +366,6 @@ WebContents.prototype.loadURL = function (url, options) { resolveAndCleanup(); } }; - const failListener = (event: Electron.Event, errorCode: number, errorDescription: string, validatedURL: string, isMainFrame: boolean) => { - if (!error && isMainFrame) { - error = { errorCode, errorDescription, url: validatedURL }; - } - }; let navigationStarted = false; let browserInitiatedInPageNavigation = false; @@ -392,6 +387,14 @@ WebContents.prototype.loadURL = function (url, options) { navigationStarted = true; } }; + const failListener = (event: Electron.Event, errorCode: number, errorDescription: string, validatedURL: string, isMainFrame: boolean) => { + if (!error && isMainFrame) { + error = { errorCode, errorDescription, url: validatedURL }; + } + if (!navigationStarted && isMainFrame) { + finishListener(); + } + }; const stopLoadingListener = () => { // By the time we get here, either 'finish' or 'fail' should have fired // if the navigation occurred. However, in some situations (e.g. when diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index 12f1c623fd..4f6185dd4c 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -519,6 +519,10 @@ describe('webContents module', () => { await expect(w.loadURL('file:non-existent')).to.eventually.be.rejected(); await expect(w.loadURL('file:non-existent')).to.eventually.be.rejected(); }); + + it('invalid URL load rejects', async () => { + await expect(w.loadURL('invalidURL')).to.eventually.be.rejected(); + }); }); describe('getFocusedWebContents() API', () => {