Skip to content

Commit

Permalink
On database error: show popup, allow user to delete and relaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Feb 21, 2019
1 parent 3fb6ab2 commit 5165eb3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
14 changes: 14 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
"description":
"Shown in the about box for the link to https://signal.org/legal"
},
"copyErrorAndQuit": {
"message": "Copy error and quit",
"description":
"Shown in the top-level error popup, allowing user to copy the error text and close the app"
},
"databaseError": {
"message": "Database Error",
"description": "Shown in a popup if the database cannot start up properly"
},
"deleteAndRestart": {
"message": "Delete all data and restart",
"description":
"Shown in a popup if the database cannot start up properly; allows user to dalete database and restart"
},
"mainMenuFile": {
"message": "&File",
"description":
Expand Down
19 changes: 12 additions & 7 deletions app/global_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,39 @@ const Errors = require('../js/modules/types/errors');
const { app, dialog, clipboard } = electron;
const { redactAll } = require('../js/modules/privacy');

// We're using hard-coded strings in this file because it needs to be ready
// to report errors before we do anything in the app. Also, we expect users to directly
// paste this text into search engines to find the bugs on GitHub.
// We use hard-coded strings until we're able to update these strings from the locale.
let quitText = 'Quit';
let copyErrorAndQuitText = 'Copy error and quit';

function handleError(prefix, error) {
console.error(`${prefix}:`, Errors.toLogFormat(error));

if (app.isReady()) {
// title field is not shown on macOS, so we don't use it
const buttonIndex = dialog.showMessageBox({
buttons: ['OK', 'Copy error'],
buttons: [quitText, copyErrorAndQuitText],
defaultId: 0,
detail: error.stack,
detail: redactAll(error.stack),
message: prefix,
noLink: true,
type: 'error',
});

if (buttonIndex === 1) {
clipboard.writeText(`${prefix}\n${redactAll(error.stack)}`);
clipboard.writeText(`${prefix}\n\n${redactAll(error.stack)}`);
}
} else {
dialog.showErrorBox(prefix, error.stack);
}

app.quit();
app.exit(1);
}

exports.updateLocale = messages => {
quitText = messages.quit.message;
copyErrorAndQuitText = messages.copyErrorAndQuit.message;
};

exports.addHandler = () => {
process.on('uncaughtException', error => {
handleError('Unhandled Error', error);
Expand Down
51 changes: 47 additions & 4 deletions app/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const sql = require('@journeyapps/sqlcipher');
const { app, dialog, clipboard } = require('electron');
const { redactAll } = require('../js/modules/privacy');
const { remove: removeUserConfig } = require('./user_config');

const pify = require('pify');
const uuidv4 = require('uuid/v4');
const { map, isString, fromPairs, forEach, last } = require('lodash');
const { map, isObject, isString, fromPairs, forEach, last } = require('lodash');

// To get long stack traces
// https://github.com/mapbox/node-sqlite3/wiki/API#sqlite3verbose
Expand Down Expand Up @@ -670,7 +674,7 @@ let db;
let filePath;
let indexedDBPath;

async function initialize({ configDir, key }) {
async function initialize({ configDir, key, messages }) {
if (db) {
throw new Error('Cannot initialize more than once!');
}
Expand All @@ -679,7 +683,10 @@ async function initialize({ configDir, key }) {
throw new Error('initialize: configDir is required!');
}
if (!isString(key)) {
throw new Error('initialize: key` is required!');
throw new Error('initialize: key is required!');
}
if (!isObject(messages)) {
throw new Error('initialize: message is required!');
}

indexedDBPath = path.join(configDir, 'IndexedDB');
Expand All @@ -705,6 +712,40 @@ async function initialize({ configDir, key }) {
await updateSchema(promisified);

db = promisified;

// test database
try {
await getMessageCount();
} catch (error) {
console.log('Database startup error:', error.stack);
const buttonIndex = dialog.showMessageBox({
buttons: [
messages.copyErrorAndQuit.message,
messages.deleteAndRestart.message,
],
defaultId: 0,
detail: redactAll(error.stack),
message: messages.databaseError.message,
noLink: true,
type: 'error',
});

if (buttonIndex === 0) {
clipboard.writeText(
`Database startup error:\n\n${redactAll(error.stack)}`
);
} else {
await close();
await removeDB();
removeUserConfig();
app.relaunch();
}

app.exit(1);
return false;
}

return true;
}

async function close() {
Expand Down Expand Up @@ -952,7 +993,9 @@ async function getConversationCount() {
const row = await db.get('SELECT count(*) from conversations;');

if (!row) {
throw new Error('getMessageCount: Unable to get count of conversations');
throw new Error(
'getConversationCount: Unable to get count of conversations'
);
}

return row['count(*)'];
Expand Down
2 changes: 2 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,8 @@
messageReceiver = null;
}

onEmpty();

window.log.warn(
'Client is no longer authorized; deleting local configuration'
);
Expand Down
13 changes: 12 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ app.on('ready', async () => {
locale = loadLocale({ appLocale, logger });
}

GlobalErrors.updateLocale(locale.messages);

let key = userConfig.get('key');
if (!key) {
console.log(
Expand All @@ -660,7 +662,15 @@ app.on('ready', async () => {
key = crypto.randomBytes(32).toString('hex');
userConfig.set('key', key);
}
await sql.initialize({ configDir: userDataPath, key });
const success = await sql.initialize({
configDir: userDataPath,
key,
messages: locale.messages,
});
if (!success) {
console.log('sql.initialize was unsuccessful; returning early');
return;
}
await sqlChannels.initialize();

try {
Expand Down Expand Up @@ -773,6 +783,7 @@ app.on('before-quit', () => {
readyForShutdown: mainWindow ? mainWindow.readyForShutdown : null,
shouldQuit: windowState.shouldQuit(),
});

windowState.markShouldQuit();
});

Expand Down

0 comments on commit 5165eb3

Please sign in to comment.