Skip to content

Commit

Permalink
add unhandled rejection handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb committed Feb 13, 2024
1 parent c574ec3 commit 855371b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/compass/src/main/handle-unhandled-rejection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { app, dialog, clipboard } from 'electron';
import cleanStack from 'clean-stack';
import ensureError from 'ensure-error';
import COMPASS_ICON from './icon';

async function handleUnhandledRejection(err: Error): Promise<void> {
// eslint-disable-next-line no-console
console.error('handling unhandledRejection', err);
err = ensureError(err);
const stack = cleanStack(err.stack || '');

const detail = `${app.getName()} version ${app.getVersion()}\nStacktrace:\n${stack}`;
const message = `${app.getName()} has encountered an unexpected error`;

// eslint-disable-next-line no-console
console.error(`${message}: ${detail}`);

const showErrorMessageBox = async () => {
const { response } = await dialog.showMessageBox({
type: 'error',
buttons: [
'OK',
process.platform === 'darwin' ? 'Copy Error' : 'Copy error',
],
icon: COMPASS_ICON,
defaultId: 0,
noLink: true,
message: message,
detail: detail,
});

if (response === 1) {
clipboard.writeText(`${message}\n${stack}`);
return;
}

if (response === 0) {
app.quit();
return;
}
};

// Dialog can't be used until app emits a `ready` event
await app.whenReady();
await showErrorMessageBox();
}

export { handleUnhandledRejection };
3 changes: 3 additions & 0 deletions packages/compass/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../setup-hadron-distribution';

import { app, dialog, crashReporter } from 'electron';
import { handleUncaughtException } from './handle-uncaught-exception';
import { handleUnhandledRejection } from './handle-unhandled-rejection';
import { initialize as initializeElectronRemote } from '@electron/remote/main';
import {
doImportConnections,
Expand Down Expand Up @@ -100,6 +101,8 @@ async function main(): Promise<void> {
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on('uncaughtException', handleUncaughtException);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.on('unhandledRejection', handleUnhandledRejection);
} else {
if (errorOutDueToAdditionalCommandLineFlags) {
process.stderr.write(
Expand Down

0 comments on commit 855371b

Please sign in to comment.