How to disable alert dialogs when errors occur


#1

I’ve been using electron as a way to replace phantomjs on my server (which has no GUI, I’m using xvfb).

I have had an issue where electron seemingly gets stuck and no errors are printed to the console. It turns out that I DID have an error but I couldn’t see it because it was going to an alert popup which was showing up in xfvb, and consequentially locks electron up (at least this is my best guess).

Is there a way to disable alert dialogs when errors occur, or better yet disable ALL dialogs? Dialogs are useless in headless mode (granted electron is not intended to be a headless app) and just get things stuck.

I’m not necessarily looking for a flag ‘disable_dialogs’. Perhaps I could override one method in the main process so that dialogs don’t open, or maybe just that they close instantly?


#2

I didn’t get an official answer so I just tried out changing dialog.showErrorBox. https://github.com/atom/electron/blob/master/docs/api/dialog.md

I just overrode the method to log to the console instead of the default behavior. Works for me.


#3

Can you explain how you override the method to log to the console?


#4
const electron = require('electron');
const dialog = electron.dialog;

// Disable error dialogs by overriding
// FIX: https://goo.gl/YsDdsS
dialog.showErrorBox = function(title, content) {
    console.log(`${title}\n${content}`);
};

#5

What file are you making these changes in?


#6

Tried above code but error dialog still pop up. By the way, I add the code at the very beginning before ready event.


#7

By inputting such script, console.log(alert.toString()), in the console of development tool, I found the following script.

function (message, title) {
    ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', toString(message), toString(title))
  }

I found that alert from renderer processes is sent through IPC, so what I want to do is to disable the original event listeners and manage the ELECTRON_BROWSER_WINDOW_ALERT event by my event listener.

Here is my solution:

ipcMain.removeAllListeners("ELECTRON_BROWSER_WINDOW_ALERT")
ipcMain.on("ELECTRON_BROWSER_WINDOW_ALERT", (event, message, title)=>{
  console.warn(`[Alert] ** ${title} ** ${message}`)
  event.returnValue = 0 // **IMPORTANT!**
})

Note that the returnValue is required to make everything works as nothing happens.
I test it in the console of my electron devTool and if there is no returnValue provided, the console will keep waiting for it without handling newly typed scripts, therefore I provide a return value equal to 0 for the synchronized IPC.