Skip to content

Commit

Permalink
Show dialog if application fails to start up properly
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Nov 26, 2018
1 parent ec12733 commit 68af1ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
47 changes: 37 additions & 10 deletions app/global_errors.js
@@ -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);
});
};
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -53,7 +53,6 @@
"config": "1.28.1",
"electron-editor-context-menu": "1.1.1",
"electron-is-dev": "0.3.0",
"electron-unhandled": "https://github.com/scottnonnenberg-signal/electron-unhandled.git#7496187472aa561d39fcd4c843a54ffbef0a388c",
"electron-updater": "2.21.10",
"emoji-datasource": "4.0.0",
"emoji-datasource-apple": "4.0.0",
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Expand Up @@ -2520,10 +2520,6 @@ electron-to-chromium@^1.2.7:
version "1.3.41"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.41.tgz#7e33643e00cd85edfd17e04194f6d00e73737235"

"electron-unhandled@https://github.com/scottnonnenberg-signal/electron-unhandled.git#7496187472aa561d39fcd4c843a54ffbef0a388c":
version "1.0.0"
resolved "https://github.com/scottnonnenberg-signal/electron-unhandled.git#7496187472aa561d39fcd4c843a54ffbef0a388c"

electron-updater@2.21.10:
version "2.21.10"
resolved "https://registry.yarnpkg.com/electron-updater/-/electron-updater-2.21.10.tgz#aa66757ebf966f4247f247a8433af45cfe8e93b0"
Expand Down

0 comments on commit 68af1ae

Please sign in to comment.