Show dialog if application fails to start up properly

This commit is contained in:
Scott Nonnenberg 2018-11-16 16:49:59 -08:00
parent ec12733dad
commit 68af1ae1ea
3 changed files with 37 additions and 15 deletions

View file

@ -1,16 +1,43 @@
const addUnhandledErrorHandler = require('electron-unhandled');
const electron = require('electron');
const Errors = require('../js/modules/types/errors');
// addHandler :: Unit -> Unit
const { app, dialog, clipboard } = electron;
// 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.
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'],
defaultId: 0,
detail: error.stack,
message: prefix,
noLink: true,
type: 'error',
});
if (buttonIndex === 1) {
clipboard.writeText(`${prefix}\n${error.stack}`);
}
} else {
dialog.showErrorBox(prefix, error.stack);
}
app.quit();
}
exports.addHandler = () => {
addUnhandledErrorHandler({
logger: error => {
console.error(
'Uncaught error or unhandled promise rejection:',
Errors.toLogFormat(error)
);
},
showDialog: false,
process.on('uncaughtException', error => {
handleError('Unhandled Error', error);
});
process.on('unhandledRejection', error => {
handleError('Unhandled Promise Rejection', error);
});
};