Prevent uncaught rejections in sql initialize

This commit is contained in:
Fedor Indutny 2021-05-07 11:42:21 -07:00 committed by GitHub
parent 0339026103
commit ade026189e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

48
main.js
View file

@ -555,15 +555,8 @@ async function createWindow() {
mainWindow.once('ready-to-show', async () => { mainWindow.once('ready-to-show', async () => {
console.log('main window is ready-to-show'); console.log('main window is ready-to-show');
try { // Ignore sql errors and show the window anyway
await sqlInitPromise; await sqlInitPromise;
} catch (error) {
console.log(
'main window is ready, but sql has errored',
error && error.stack
);
return;
}
if (!mainWindow) { if (!mainWindow) {
return; return;
@ -579,9 +572,12 @@ async function createWindow() {
// Renderer asks if we are done with the database // Renderer asks if we are done with the database
ipc.on('database-ready', async event => { ipc.on('database-ready', async event => {
try { const { error } = await sqlInitPromise;
await sqlInitPromise; if (error) {
} catch (error) { console.log(
'database-ready requested, but got sql error',
error && error.stack
);
return; return;
} }
@ -1039,11 +1035,18 @@ async function initializeSQL() {
} }
sqlInitTimeStart = Date.now(); sqlInitTimeStart = Date.now();
await sql.initialize({ try {
configDir: userDataPath, await sql.initialize({
key, configDir: userDataPath,
}); key,
sqlInitTimeEnd = Date.now(); });
} catch (error) {
return { ok: false, error };
} finally {
sqlInitTimeEnd = Date.now();
}
return { ok: true };
} }
const sqlInitPromise = initializeSQL(); const sqlInitPromise = initializeSQL();
@ -1166,7 +1169,7 @@ app.on('ready', async () => {
loadingWindow.once('ready-to-show', async () => { loadingWindow.once('ready-to-show', async () => {
loadingWindow.show(); loadingWindow.show();
// Wait for sql initialization to complete // Wait for sql initialization to complete, but ignore errors
await sqlInitPromise; await sqlInitPromise;
loadingWindow.destroy(); loadingWindow.destroy();
loadingWindow = null; loadingWindow = null;
@ -1178,9 +1181,8 @@ app.on('ready', async () => {
// Run window preloading in parallel with database initialization. // Run window preloading in parallel with database initialization.
await createWindow(); await createWindow();
try { const { error: sqlError } = await sqlInitPromise;
await sqlInitPromise; if (sqlError) {
} catch (error) {
console.log('sql.initialize was unsuccessful; returning early'); console.log('sql.initialize was unsuccessful; returning early');
const buttonIndex = dialog.showMessageBoxSync({ const buttonIndex = dialog.showMessageBoxSync({
buttons: [ buttons: [
@ -1188,7 +1190,7 @@ app.on('ready', async () => {
locale.messages.deleteAndRestart.message, locale.messages.deleteAndRestart.message,
], ],
defaultId: 0, defaultId: 0,
detail: redactAll(error.stack), detail: redactAll(sqlError.stack),
message: locale.messages.databaseError.message, message: locale.messages.databaseError.message,
noLink: true, noLink: true,
type: 'error', type: 'error',
@ -1196,7 +1198,7 @@ app.on('ready', async () => {
if (buttonIndex === 0) { if (buttonIndex === 0) {
clipboard.writeText( clipboard.writeText(
`Database startup error:\n\n${redactAll(error.stack)}` `Database startup error:\n\n${redactAll(sqlError.stack)}`
); );
} else { } else {
await sql.sqlCall('removeDB', []); await sql.sqlCall('removeDB', []);