From 488ac228602fd609455b1e423bc4faae991fc4e8 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Tue, 24 Aug 2021 13:31:06 -0700 Subject: [PATCH] Display dialog on corrupted database --- main.js | 59 ++++++++++++++++++++++++++++++------------------ ts/sql/Client.ts | 2 +- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index c56ff42d3..c39e28159 100644 --- a/main.js +++ b/main.js @@ -1143,6 +1143,42 @@ async function initializeSQL() { const sqlInitPromise = initializeSQL(); +const onDatabaseError = async error => { + const buttonIndex = dialog.showMessageBoxSync({ + buttons: [ + locale.messages.copyErrorAndQuit.message, + locale.messages.deleteAndRestart.message, + ], + defaultId: 0, + detail: redactAll(error), + message: locale.messages.databaseError.message, + noLink: true, + type: 'error', + }); + + if (buttonIndex === 0) { + clipboard.writeText(`Database startup error:\n\n${redactAll(error)}`); + } else { + await sql.removeDB(); + removeUserConfig(); + app.relaunch(); + } + + app.exit(1); +}; + +ipc.on('database-error', (event, error) => { + if (mainWindow) { + mainWindow.close(); + } + mainWindow = undefined; + + // Prevent window from re-opening + ready = false; + + onDatabaseError(error); +}); + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. @@ -1300,29 +1336,8 @@ app.on('ready', async () => { const { error: sqlError } = await sqlInitPromise; if (sqlError) { console.log('sql.initialize was unsuccessful; returning early'); - const buttonIndex = dialog.showMessageBoxSync({ - buttons: [ - locale.messages.copyErrorAndQuit.message, - locale.messages.deleteAndRestart.message, - ], - defaultId: 0, - detail: redactAll(sqlError.stack), - message: locale.messages.databaseError.message, - noLink: true, - type: 'error', - }); - if (buttonIndex === 0) { - clipboard.writeText( - `Database startup error:\n\n${redactAll(sqlError.stack)}` - ); - } else { - await sql.removeDB(); - removeUserConfig(); - app.relaunch(); - } - - app.exit(1); + await onDatabaseError(sqlError.stack); return; } diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 82b7f0bcb..61fa014f7 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -448,7 +448,7 @@ function _updateJob(id: number, data: ClientJobUpdateType) { window.log.error( `Detected corruption. Restarting the application immediately. Error: ${error.message}` ); - window.restart(); + ipcRenderer?.send('database-error', error.message); } return reject(error);