diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 9acdae57be..b3193bbb23 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3609,18 +3609,26 @@ v8::Local WebContents::TakeHeapSnapshot( flags = base::File::AddFlagsForPassingToUntrustedProcess(flags); base::File file(file_path, flags); if (!file.IsValid()) { - promise.RejectWithErrorMessage("takeHeapSnapshot failed"); + promise.RejectWithErrorMessage( + "Failed to take heap snapshot with invalid file path " + +#if BUILDFLAG(IS_WIN) + base::WideToUTF8(file_path.value())); +#else + file_path.value()); +#endif return handle; } auto* frame_host = web_contents()->GetPrimaryMainFrame(); if (!frame_host) { - promise.RejectWithErrorMessage("takeHeapSnapshot failed"); + promise.RejectWithErrorMessage( + "Failed to take heap snapshot with invalid webContents main frame"); return handle; } if (!frame_host->IsRenderFrameLive()) { - promise.RejectWithErrorMessage("takeHeapSnapshot failed"); + promise.RejectWithErrorMessage( + "Failed to take heap snapshot with nonexistent render frame"); return handle; } @@ -3640,7 +3648,7 @@ v8::Local WebContents::TakeHeapSnapshot( if (success) { promise.Resolve(); } else { - promise.RejectWithErrorMessage("takeHeapSnapshot failed"); + promise.RejectWithErrorMessage("Failed to take heap snapshot"); } }, base::Owned(std::move(electron_renderer)), std::move(promise))); diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents-spec.ts index ae5404912d..650c8c1e45 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents-spec.ts @@ -1803,8 +1803,24 @@ describe('webContents module', () => { await w.loadURL('about:blank'); - const promise = w.webContents.takeHeapSnapshot(''); - return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed'); + const badPath = path.join('i', 'am', 'a', 'super', 'bad', 'path'); + const promise = w.webContents.takeHeapSnapshot(badPath); + return expect(promise).to.be.eventually.rejectedWith(Error, `Failed to take heap snapshot with invalid file path ${badPath}`); + }); + + it('fails with invalid render process', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true + } + }); + + const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot'); + + w.webContents.destroy(); + const promise = w.webContents.takeHeapSnapshot(filePath); + return expect(promise).to.be.eventually.rejectedWith(Error, 'Failed to take heap snapshot with nonexistent render frame'); }); });