2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2021-06-18 17:04:27 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { app, dialog, clipboard } from 'electron';
|
2024-08-20 19:45:43 +00:00
|
|
|
import os from 'node:os';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2021-07-14 23:39:52 +00:00
|
|
|
import * as Errors from '../ts/types/errors';
|
2021-06-18 17:04:27 +00:00
|
|
|
import { redactAll } from '../ts/util/privacy';
|
|
|
|
import { reallyJsonStringify } from '../ts/util/reallyJsonStringify';
|
2022-09-27 21:01:06 +00:00
|
|
|
import type { LocaleType } from './locale';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
// 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: string, error: Error): void {
|
2022-11-22 18:43:43 +00:00
|
|
|
const formattedError = Errors.toLogFormat(error);
|
2021-06-18 17:04:27 +00:00
|
|
|
if (console._error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
console._error(`${prefix}:`, formattedError);
|
2021-06-18 17:04:27 +00:00
|
|
|
}
|
2022-11-22 18:43:43 +00:00
|
|
|
console.error(`${prefix}:`, formattedError);
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
if (app.isReady()) {
|
|
|
|
// title field is not shown on macOS, so we don't use it
|
|
|
|
const buttonIndex = dialog.showMessageBoxSync({
|
|
|
|
buttons: [quitText, copyErrorAndQuitText],
|
|
|
|
defaultId: 0,
|
2022-11-22 18:43:43 +00:00
|
|
|
detail: redactAll(formattedError),
|
2021-06-18 17:04:27 +00:00
|
|
|
message: prefix,
|
|
|
|
noLink: true,
|
|
|
|
type: 'error',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (buttonIndex === 1) {
|
2024-08-20 19:45:43 +00:00
|
|
|
clipboard.writeText(
|
|
|
|
`${prefix}\n\n${redactAll(formattedError)}\n\n` +
|
|
|
|
`App Version: ${app.getVersion()}\n` +
|
|
|
|
`OS: ${os.platform()}`
|
|
|
|
);
|
2021-06-18 17:04:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-22 18:43:43 +00:00
|
|
|
dialog.showErrorBox(prefix, formattedError);
|
2021-06-18 17:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.exit(1);
|
|
|
|
}
|
|
|
|
|
2022-09-27 21:01:06 +00:00
|
|
|
export const updateLocale = (locale: LocaleType): void => {
|
2023-03-30 00:03:25 +00:00
|
|
|
quitText = locale.i18n('icu:quit');
|
|
|
|
copyErrorAndQuitText = locale.i18n('icu:copyErrorAndQuit');
|
2021-06-18 17:04:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function _getError(reason: unknown): Error {
|
|
|
|
if (reason instanceof Error) {
|
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorString = reallyJsonStringify(reason);
|
|
|
|
return new Error(`Promise rejected with a non-error: ${errorString}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const addHandler = (): void => {
|
2022-06-30 19:00:40 +00:00
|
|
|
app.on('render-process-gone', (_event, _webContents, details) => {
|
|
|
|
const { reason, exitCode } = details;
|
|
|
|
|
|
|
|
if (reason === 'clean-exit') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(
|
|
|
|
'Render process is gone',
|
|
|
|
new Error(`Reason: ${reason}, Exit Code: ${exitCode}`)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
process.on('uncaughtException', (reason: unknown) => {
|
|
|
|
handleError('Unhandled Error', _getError(reason));
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason: unknown) => {
|
|
|
|
handleError('Unhandled Promise Rejection', _getError(reason));
|
|
|
|
});
|
|
|
|
};
|