#Throwing an error in main process does not exit the app - whats the best practice?

11 messages · Page 1 of 1 (latest)

desert kestrel
#

When I throw an error directly in the main process, I get an error dialog, but the electron process does not quit after confirming the dialog.

throw new Error('test');

When I throw an error in an async IIFE (to be able to await the app ready event), I don't get a dialog, the process also does not exit:

void (async () => {
    await app.whenReady();
    throw new Error('test');
})();

What's the best practice to handle an error in the main process?
I could catch() the error and app.quit() the process or add a process.on('uncaughtException') handler but there is no dialog for the user that something went wrong.

desert kestrel
#

seems I can do something like this:

process.on('unhandledRejection', () => {
    dialog.showMessageBoxSync({
        type: 'error',
        title: 'Error in Main process',
        message: 'Something failed'
    });

    app.exit(1);
});

then I get a dialog and after confirming, the app exits

cursive bone
#
  1. Learn about JS concurrency and error handling
  2. Review your logic to prevent the possibility of unhandledRejection events. This event is a red flag of fundamentally incorrect code logic
  3. Handle errors based on its critically level. It's completely up to your product.
  4. app.quit() is a graceful quit when everything is good. app.exit{} is when some critical stuff happened
desert kestrel
#

if I throw an error in the main process, it's an unhandled exception in this case, it's not like I can prevent the possibility of an unhandled rejection

#

I can try/catch the main function, but in this case when the start of the app somehow fails, I want to throw

#

but I also want to have a notice to the user and not silently keep the app open without a window

cursive bone
# desert kestrel I can try/catch the main function, but in this case when the start of the app so...

You can prevent the possibility of any error but you can do it with the logic of your app. Add catch block in places where your business logic is called and provide the error handling logic you want.

Electron already shows error dialogs on windows and MacOS if some bad stuff happened before the app ready event. On Linux Electron sends error to stderr instead.

If you for some reason want to provide your own error handling for incorrect initialization then provide .catch() for app.whenReady() as well as manually handle errors in each app.on("some-event") listeners

cursive bone
desert kestrel
#

I was just wondering why electron shows no error dialog and keeps processes open without a main window when I throw an error somewhere after the app ready event

cursive bone