Show uncaughtException as an internal error and quit the app

This commit is contained in:
Yuya Ochiai
2017-10-17 00:54:56 +09:00
parent 2a9ecaf119
commit a9af3c2dcb
2 changed files with 77 additions and 10 deletions

View File

@@ -10,15 +10,18 @@ const {
systemPreferences,
session
} = require('electron');
const os = require('os');
const path = require('path');
const isDev = require('electron-is-dev');
const installExtension = require('electron-devtools-installer');
const squirrelStartup = require('./main/squirrelStartup');
const CriticalErrorHandler = require('./main/CriticalErrorHandler');
const protocols = require('../electron-builder.json').protocols;
process.on('uncaughtException', (error) => {
console.error(error);
});
const criticalErrorHandler = new CriticalErrorHandler();
process.on('uncaughtException', criticalErrorHandler.processUncaughtExceptionHandler.bind(criticalErrorHandler));
global.willAppQuit = false;
@@ -27,9 +30,6 @@ if (squirrelStartup()) {
global.willAppQuit = true;
}
const os = require('os');
const path = require('path');
var settings = require('./common/settings');
var certificateStore = require('./main/certificateStore').load(path.resolve(app.getPath('userData'), 'certificate.json'));
const {createMainWindow} = require('./main/mainWindow');
@@ -304,6 +304,10 @@ app.on('certificate-error', (event, webContents, url, error, certificate, callba
}
});
app.on('gpu-process-crashed', () => {
throw new Error('The GPU process has crached');
});
const loginCallbackMap = new Map();
ipcMain.on('login-credentials', (event, request, user, password) => {
@@ -392,11 +396,10 @@ app.on('ready', () => {
// when you should delete the corresponding element.
mainWindow = null;
});
mainWindow.on('unresponsive', () => {
console.log('The application has become unresponsive.');
});
criticalErrorHandler.setMainWindow(mainWindow);
mainWindow.on('unresponsive', criticalErrorHandler.windowUnresponsiveHandler.bind(criticalErrorHandler));
mainWindow.webContents.on('crashed', () => {
console.log('The application has crashed.');
throw new Error('webContents \'crashed\' event has been emitted');
});
ipcMain.on('notified', () => {

View File

@@ -0,0 +1,64 @@
const {app, dialog, shell} = require('electron');
const fs = require('fs');
const os = require('os');
const path = require('path');
function createErrorReport(err) {
return `Application: ${app.getName()} ${app.getVersion()}\n` +
`Platform: ${os.type()} ${os.release()} ${os.arch()}\n` +
`${err.stack}`;
}
function bindWindowToShowMessageBox(win) {
if (win && win.isVisible()) {
return dialog.showMessageBox.bind(null, win);
}
return dialog.showMessageBox;
}
class CriticalErrorHandler {
constructor() {
this.mainWindow = null;
}
setMainWindow(mainWindow) {
this.mainWindow = mainWindow;
}
windowUnresponsiveHandler() {
const result = dialog.showMessageBox(this.mainWindow, {
type: 'warning',
title: `Unresponsive - ${app.getName()}`,
message: 'The window is no longer responsive.\nDo you wait until the window becomes responsive again?',
buttons: ['No', 'Yes'],
defaultId: 0
});
if (result === 0) {
throw new Error('BrowserWindow \'unresponsive\' event has been emitted');
}
}
processUncaughtExceptionHandler(err) {
const file = path.join(app.getPath('userData'), `uncaughtException-${Date.now()}.txt`);
const report = createErrorReport(err);
fs.writeFileSync(file, report.replace(new RegExp('\\n', 'g'), os.EOL));
fs.writeSync(2, `See "${file}" to report the problem.\n`);
if (app.isReady()) {
const showMessageBox = bindWindowToShowMessageBox(this.mainWindow);
const result = showMessageBox({
type: 'error',
title: `Error - ${app.getName()}`,
message: `An internal error has occurred: ${err.message}\nThe application will quit.`,
buttons: ['OK', 'Show detail'],
defaultId: 0
});
if (result === 1) {
shell.openExternal(file);
}
}
throw err;
}
}
module.exports = CriticalErrorHandler;