From 2a5f7e3fecc98debea2b3408662d402a1e1681a0 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 28 Feb 2022 01:13:06 +0100 Subject: feat: Handle service load failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a "failed" state for services where the BrowserView and WebContents should be left around to keep history and allow people to navigate back. Access to the browser history otherwise doesn't seem possible (see https://github.com/electron/electron/issues/26727 and https://github.com/electron/electron/issues/7186), so destroying BrowserView and managing our own history is not possible. Also keep https://github.com/electron/electron/issues/24113 in mind. Signed-off-by: Kristóf Marussy --- .../electron/impl/ElectronServiceView.ts | 36 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts') diff --git a/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts b/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts index e5fdf11..d90ff19 100644 --- a/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts +++ b/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts @@ -22,12 +22,15 @@ import type { BrowserViewBounds } from '@sophie/shared'; import { BrowserView } from 'electron'; import type Service from '../../../stores/Service'; +import { getLogger } from '../../../utils/log'; import type Resources from '../../resources/Resources'; import type { ServiceView } from '../types'; import ElectronPartition from './ElectronPartition'; import type ElectronViewFactory from './ElectronViewFactory'; +const log = getLogger('ElectronServiceView'); + export default class ElectronServiceView implements ServiceView { readonly id: string; @@ -72,20 +75,39 @@ export default class ElectronServiceView implements ServiceView { } }); + webContents.on( + 'did-fail-load', + (_event, errorCode, errorDesc, url, isMainFrame) => { + if (isMainFrame) { + setLocation(url); + service.setFailed(errorCode, errorDesc); + log.warn( + 'Failed to load', + url, + 'in service', + this.id, + errorCode, + errorDesc, + ); + } + }, + ); + webContents.on('page-title-updated', (_event, title) => { service.setTitle(title); }); webContents.on('did-start-loading', () => { - service.startedLoading(); + service.startLoading(); }); webContents.on('did-stop-loading', () => { - service.finishedLoading(); + service.finishLoading(); }); - webContents.on('render-process-gone', () => { - service.crashed(); + webContents.on('render-process-gone', (_event, details) => { + const { reason, exitCode } = details; + service.setCrashed(reason, exitCode); }); } @@ -93,8 +115,10 @@ export default class ElectronServiceView implements ServiceView { return this.browserView.webContents.id; } - loadURL(url: string): Promise { - return this.browserView.webContents.loadURL(url); + loadURL(url: string): void { + this.browserView.webContents.loadURL(url).catch((error) => { + log.warn('Error while loading', url, 'in service', this.id, error); + }); } goBack(): void { -- cgit v1.2.3-70-g09d2