From c073812ecc880e01f798ff9ddf1a20adaf8e560e Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Wed, 19 Apr 2017 09:25:06 -0700 Subject: [PATCH 1/2] Listen to electron crash events --- client/main-window/main-window.ts | 18 +++++++++++++++++- client/main.ts | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/client/main-window/main-window.ts b/client/main-window/main-window.ts index c431856b2f..b97ccc2dbe 100644 --- a/client/main-window/main-window.ts +++ b/client/main-window/main-window.ts @@ -30,7 +30,7 @@ export class MainWindow extends UniqueWindow { }, }); const url = process.env.HOT ? devServerUrl : buildFileUrl; - + this._setupEvents(window); window.loadURL(url); const anyWindow = window as any; @@ -59,4 +59,20 @@ export class MainWindow extends UniqueWindow { return window; } + + private _setupEvents(window: Electron.BrowserWindow) { + window.webContents.on("crashed", (error) => { + logger.error("There was a crash", error); + }); + + window.webContents.on("did-fail-load", (error) => { + windows.splashScreen.updateMessage( + "Fail to load! Make sure you built the app or are running the dev-server."); + logger.error("Fail to load", error); + }); + + window.on("unresponsive", (error) => { + logger.error("There was a crash", error); + }); + } } diff --git a/client/main.ts b/client/main.ts index b14c93571b..ed2abdd105 100644 --- a/client/main.ts +++ b/client/main.ts @@ -2,6 +2,7 @@ import { app, protocol } from "electron"; import * as path from "path"; import { windows } from "./core"; +import { logger } from "./logger"; app.setPath("userData", path.join(app.getPath("appData"), "batch-labs")); @@ -40,3 +41,7 @@ app.on("activate", () => { createWindow(); } }); + +process.on("uncaughtException", (error) => { + logger.error("There was a uncaught exception", error); +}); From 27d2a321db62db166ac5d9883c7f336eec220b5e Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Wed, 24 May 2017 11:57:17 -0700 Subject: [PATCH 2/2] Show recover window on error --- client/client-constants.ts | 4 ++++ client/core/global.ts | 4 +++- client/main-window/main-window.ts | 6 ++++-- client/main.ts | 22 ++++++++++++++++++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/client/client-constants.ts b/client/client-constants.ts index 58311e8a79..bb9576b2d6 100644 --- a/client/client-constants.ts +++ b/client/client-constants.ts @@ -17,6 +17,10 @@ const urls = { dev: `file://${root}/client/splash-screen/splash-screen.html`, prod: `file://${root}/build/client/splash-screen/splash-screen.html`, }, + recover: { + dev: `file://${root}/client/recover-window/recover-window.html`, + prod: `file://${root}/build/client/recover-window/recover-window.html`, + }, icon: __dirname + "/../assets/images/labs.ico", }; diff --git a/client/core/global.ts b/client/core/global.ts index d7ce19cabb..4abf5e7bc0 100644 --- a/client/core/global.ts +++ b/client/core/global.ts @@ -1,12 +1,14 @@ import { AuthenticationWindow } from "../authentication"; import { MainWindow } from "../main-window"; +import { RecoverWindow } from "../recover-window"; import { SplashScreen } from "../splash-screen"; const splashScreen = new SplashScreen(); const authenticationWindow = new AuthenticationWindow(); +const recoverWindow = new RecoverWindow(); const mainWindow = new MainWindow(); /** * Set of unique windows used across the app */ -export const windows = { splashScreen, authentication: authenticationWindow, main: mainWindow }; +export const windows = { splashScreen, authentication: authenticationWindow, main: mainWindow, recover: recoverWindow }; diff --git a/client/main-window/main-window.ts b/client/main-window/main-window.ts index b97ccc2dbe..6e341630f0 100644 --- a/client/main-window/main-window.ts +++ b/client/main-window/main-window.ts @@ -61,8 +61,9 @@ export class MainWindow extends UniqueWindow { } private _setupEvents(window: Electron.BrowserWindow) { - window.webContents.on("crashed", (error) => { + window.webContents.on("crashed", (error: Error) => { logger.error("There was a crash", error); + windows.recover.createWithError(error.message); }); window.webContents.on("did-fail-load", (error) => { @@ -71,8 +72,9 @@ export class MainWindow extends UniqueWindow { logger.error("Fail to load", error); }); - window.on("unresponsive", (error) => { + window.on("unresponsive", (error: Error) => { logger.error("There was a crash", error); + windows.recover.createWithError(error.message); }); } } diff --git a/client/main.ts b/client/main.ts index ed2abdd105..bef2c43af5 100644 --- a/client/main.ts +++ b/client/main.ts @@ -1,4 +1,4 @@ -import { app, protocol } from "electron"; +import { app, ipcMain, protocol } from "electron"; import * as path from "path"; import { windows } from "./core"; @@ -42,6 +42,24 @@ app.on("activate", () => { } }); -process.on("uncaughtException", (error) => { +ipcMain.once("exit", () => { + process.exit(1); +}); + +ipcMain.on("reload", () => { + // Destroy window and error window if applicable + windows.main.destroy(); + windows.recover.destroy(); + + // Show splash screen + windows.splashScreen.create(); + windows.splashScreen.updateMessage("Loading app"); + + // Reopen a new window + windows.main.create(); +}); + +process.on("uncaughtException", (error: Error) => { logger.error("There was a uncaught exception", error); + windows.recover.createWithError(error.message); });