Skip to content

Commit

Permalink
fix: don't double-log unhandled rejections (#37501)
Browse files Browse the repository at this point in the history
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Mar 14, 2023
1 parent c1b8848 commit 4382f2a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shell/browser/electron_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void ElectronBrowserMainParts::PostEarlyInitialization() {
env->set_trace_sync_io(env->options()->trace_sync_io);

// We do not want to crash the main process on unhandled rejections.
env->options()->unhandled_rejections = "warn";
env->options()->unhandled_rejections = "warn-with-error-code";

// Add Electron extended APIs.
electron_bindings_->BindTo(js_env_->isolate(), env->process_object());
Expand Down
2 changes: 1 addition & 1 deletion shell/renderer/electron_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void ElectronRendererClient::DidCreateScriptContext(
env->options()->force_context_aware = true;

// We do not want to crash the renderer process on unhandled rejections.
env->options()->unhandled_rejections = "warn";
env->options()->unhandled_rejections = "warn-with-error-code";

environments_.insert(env);

Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/api/unhandled-rejection-handled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { app } = require('electron');

const handleUnhandledRejection = (reason) => {
console.error(`Unhandled Rejection: ${reason.stack}`);
app.quit();
};

const main = async () => {
process.on('unhandledRejection', handleUnhandledRejection);
throw new Error('oops');
};

main();
5 changes: 5 additions & 0 deletions spec/fixtures/api/unhandled-rejection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const main = async () => {
throw new Error('oops');
};

main();
36 changes: 36 additions & 0 deletions spec/node-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,42 @@ describe('node feature', () => {
}));
expect(result).to.equal('hello');
});

it('does not log the warning more than once when the rejection is unhandled', async () => {
const appPath = path.join(mainFixturesPath, 'api', 'unhandled-rejection.js');
const appProcess = childProcess.spawn(process.execPath, [appPath]);

let output = '';
const out = (data: string) => {
output += data;
if (/UnhandledPromiseRejectionWarning/.test(data)) {
appProcess.kill();
}
};
appProcess.stdout!.on('data', out);
appProcess.stderr!.on('data', out);

await emittedOnce(appProcess, 'exit');
expect(/UnhandledPromiseRejectionWarning/.test(output)).to.equal(true);
const matches = output.match(/Error: oops/gm);
expect(matches).to.have.lengthOf(1);
});

it('does not log the warning more than once when the rejection is handled', async () => {
const appPath = path.join(mainFixturesPath, 'api', 'unhandled-rejection-handled.js');
const appProcess = childProcess.spawn(process.execPath, [appPath]);

let output = '';
const out = (data: string) => { output += data; };
appProcess.stdout!.on('data', out);
appProcess.stderr!.on('data', out);

const [code] = await emittedOnce(appProcess, 'exit');
expect(code).to.equal(0);
expect(/UnhandledPromiseRejectionWarning/.test(output)).to.equal(false);
const matches = output.match(/Error: oops/gm);
expect(matches).to.have.lengthOf(1);
});
});
});

Expand Down

0 comments on commit 4382f2a

Please sign in to comment.