Is it possible to catch exceptions of renderer processes in electrons main process?

后端 未结 2 950
灰色年华
灰色年华 2021-01-11 18:20

I\'m using Electrons Quick Start Projekt (Commit dbef48ee7d072a38724ecfa57601e39d36e9714e) to test exceptions.

In index.html I changed the name of the r

相关标签:
2条回答
  • 2021-01-11 19:04

    I had a similar issue where I wanted to log errors to a file from the main process. Here's an addition to the answer already provided by Teak:

    var ipc = require('electron').ipcRenderer;
    window.onerror = function(error, url, line) {
        ipc.send('errorInWindow', error);
    };
    

    would work. Just keep in mind that the onerror callback passes 5 arguments, where the last one is the actual Error object.

    https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror

    However, since messages are serialized when sending through IPC, it's not possible to pass the Error object fully since it won't serialize correctly by default. Thus the data needs to be massaged before sending it if you need more error details (such as stack trace etc).

    I used the following Is it not possible to stringify an Error using JSON.stringify? for some ideas and the end result was:

    var objFromError = function(err, filter, space) {
      var plainObject = {};
      Object.getOwnPropertyNames(err).forEach(function(key) {
        plainObject[key] = err[key];
      });
    
      return plainObject;
    };
    
    window.onerror = function (msg, url, lineNo, columnNo, error) {
      ipcRenderer.send('asynchronous-windowerr', 'main', objFromError(error));
    }
    

    Then in main.js:

    ipcMain.on('asynchronous-windowerr', function(event, source, err) {
        var str = source + ': ';
    
        if(err != null) {
            if(err.stack != null) {
                str += err.stack;
            } else if(err.message != null) {
                str += err.message;
            }
        }
    
        loggerr.appendLogFile(errLogFile, 'err', str);
    })
    
    0 讨论(0)
  • 2021-01-11 19:06

    Electron windows are rendered in their own process. Because of this there is little if any communication between main process and render processes. The best you can do is catch errors in the render process and use Electrons IPC module to pass them back to your main process.

    In your render process:

    var ipc = require('electron').ipcRenderer;
    window.onerror = function(error, url, line) {
        ipc.send('errorInWindow', error);
    };
    

    In your main process:

    var ipc = require('electron').ipcMain;
    
    ipc.on('errorInWindow', function(event, data){
        console.log(data)
    });
    

    Additionally your main process can watch for a limited set of events directly on the window (or on the windows webContents):

    window.on('unresponsive', function() {
        console.log('window crashed');
    });
    

    ...

    window.webContents.on('did-fail-load', function() {
        console.log('window failed load');
    });
    
    0 讨论(0)
提交回复
热议问题