Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve contents.takeHeapSnapshot error messages #37461

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3514,18 +3514,26 @@ v8::Local<v8::Promise> 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;
}

Expand All @@ -3545,7 +3553,7 @@ v8::Local<v8::Promise> 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)));
Expand Down
20 changes: 18 additions & 2 deletions spec/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,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');
});
});

Expand Down