chore: improve contents.takeHeapSnapshot error messages (#37434)

* chore: improve contents.takeHeapSnapshot error messages

* fix wstring conversion issue
This commit is contained in:
Shelley Vohr 2023-03-01 16:50:36 +01:00 committed by GitHub
parent 8f2917db01
commit 9b20b3a722
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 6 deletions

View file

@ -3609,18 +3609,26 @@ v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
flags = base::File::AddFlagsForPassingToUntrustedProcess(flags); flags = base::File::AddFlagsForPassingToUntrustedProcess(flags);
base::File file(file_path, flags); base::File file(file_path, flags);
if (!file.IsValid()) { 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; return handle;
} }
auto* frame_host = web_contents()->GetPrimaryMainFrame(); auto* frame_host = web_contents()->GetPrimaryMainFrame();
if (!frame_host) { if (!frame_host) {
promise.RejectWithErrorMessage("takeHeapSnapshot failed"); promise.RejectWithErrorMessage(
"Failed to take heap snapshot with invalid webContents main frame");
return handle; return handle;
} }
if (!frame_host->IsRenderFrameLive()) { if (!frame_host->IsRenderFrameLive()) {
promise.RejectWithErrorMessage("takeHeapSnapshot failed"); promise.RejectWithErrorMessage(
"Failed to take heap snapshot with nonexistent render frame");
return handle; return handle;
} }
@ -3640,7 +3648,7 @@ v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
if (success) { if (success) {
promise.Resolve(); promise.Resolve();
} else { } else {
promise.RejectWithErrorMessage("takeHeapSnapshot failed"); promise.RejectWithErrorMessage("Failed to take heap snapshot");
} }
}, },
base::Owned(std::move(electron_renderer)), std::move(promise))); base::Owned(std::move(electron_renderer)), std::move(promise)));

View file

@ -1803,8 +1803,24 @@ describe('webContents module', () => {
await w.loadURL('about:blank'); await w.loadURL('about:blank');
const promise = w.webContents.takeHeapSnapshot(''); const badPath = path.join('i', 'am', 'a', 'super', 'bad', 'path');
return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed'); 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');
}); });
}); });