Skip to content
This repository has been archived by the owner on Jul 12, 2021. It is now read-only.

Commit

Permalink
Log errors to file
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Jan 2, 2020
1 parent 55bf208 commit 2e06520
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
"draft-js-plugins-editor": "^3.0.0",
"electron-context-menu": "^0.15.2",
"electron-debug": "^3.0.0",
"electron-log": "^4.0.2",
"electron-md-to-pdf": "^2.2.0",
"electron-settings": "^3.2.0",
"electron-unhandled": "^3.0.1",
"electron-updater": "^4.0.14",
"electron-util": "^0.13.1",
"feather-icons": "^4.25.0",
Expand Down
3 changes: 2 additions & 1 deletion src/main/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from "electron";
import logger from "electron-log";

import { Translations } from "../../shared/types";
import translationsDe from "./translations/de";
Expand Down Expand Up @@ -86,7 +87,7 @@ export function translate(
// Log error and return `i18nKey` if translation string is missing both in current and fallback
// language
if (!translation) {
console.error(`Missing translation of i18nKey "${i18nKey}"`);
logger.error(`Missing translation of i18nKey "${i18nKey}"`);
return i18nKey;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import contextMenu from "electron-context-menu";
import electronDebug from "electron-debug";
import path from "path";

import { initLogger } from "../shared/logger";
import { initI18n } from "./i18n/i18n";
import initIpcListeners from "./ipcMain/listeners";
import { buildMenu } from "./menu/menu";
import updateApp from "./updater";
import { getWindow, setWindow } from "./window";

initLogger();
electronDebug();
contextMenu();

Expand Down
9 changes: 6 additions & 3 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logger from "electron-log";
import { autoUpdater } from "electron-updater";
import { is } from "electron-util";

autoUpdater.logger = logger;

/**
* Check if the app has updates available. If so, download the latest one and notify the user
*/
Expand All @@ -9,10 +12,10 @@ export default async function updateApp(): Promise<void> {
if (!is.macAppStore) {
try {
await autoUpdater.checkForUpdatesAndNotify();
} catch (e) {
} catch (err) {
// Ignore errors thrown because user is not connected to internet
if (e.message !== "net::ERR_INTERNET_DISCONNECTED") {
throw e;
if (err.message !== "net::ERR_INTERNET_DISCONNECTED") {
throw err;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { remote } from "electron";
import logger from "electron-log";
import React, { PureComponent, ReactNode } from "react";

import { translate, translations } from "../../../../utils/i18n";
Expand Down Expand Up @@ -48,7 +49,9 @@ type Props = StateProps & DispatchProps;

export default class ImportOverlay extends PureComponent<Props, {}> {
static showImportFormatError(): void {
remote.dialog.showErrorBox(translations["import-error-title"], "No import format selected");
const errMsg = "No import format selected";
logger.error(`Import error: ${errMsg}`);
remote.dialog.showErrorBox(translations["import-error-title"], errMsg);
}

constructor(props: Props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { remote } from "electron";
import logger from "electron-log";
import { is } from "electron-util";
import React, { ChangeEvent, PureComponent, ReactNode } from "react";

Expand Down Expand Up @@ -123,6 +124,7 @@ export default class PrefOverlay extends PureComponent<Props, State> {
try {
moveFile(fileDir, `${newDir}/${FILE_NAME}`);
} catch (err) {
logger.error("Error while moving diary file: ", err);
remote.dialog.showErrorBox(
translations["move-error-title"],
`${translations["move-error-msg"]}: ${err.message}`,
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";

import { initLogger } from "../shared/logger";
import AppContainer from "./components/AppContainer";
import initIpcListeners from "./electron/ipcRenderer/listeners";
import store from "./store/store";
import { initI18n } from "./utils/i18n";

initLogger();
initIpcListeners();
initI18n();

Expand Down
6 changes: 4 additions & 2 deletions src/renderer/store/file/actionCreators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from "electron-log";

import { disableMenuItems, enableMenuItems } from "../../electron/ipcRenderer/senders";
import createBackup from "../../files/diary/backupFile";
import { getDiaryFilePath, getMetadata } from "../../files/diary/diaryFile";
Expand Down Expand Up @@ -180,7 +182,7 @@ export const createEncryptedFile = (password: string): ThunkActionT => (dispatch
createIndex(entries);
enableMenuItems();
} catch (err) {
console.error(err);
logger.error(err);
dispatch(setEncryptError(err.message));
}
};
Expand All @@ -201,7 +203,7 @@ const writeEntriesEncrypted = (entries: Entries, hashedPassword: string): ThunkA
writeEncryptedFile(filePath, hashedPassword, JSON.stringify(fileContent));
dispatch(setEncryptSuccess(entries));
} catch (err) {
console.error(err);
logger.error(err);
dispatch(setEncryptError(err.message));
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/shared/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logger from "electron-log";
import unhandled from "electron-unhandled";

export function initLogger(): void {
// Set log level for writing to file
logger.transports.file.level = "info";

// Catch unhandled errors and promise rejections, log them instead of crashing
unhandled({
logger: err => logger.error(err),
showDialog: false,
});
}
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3005,7 +3005,7 @@ [email protected]:
dependencies:
source-map "~0.6.0"

clean-stack@^2.0.0:
clean-stack@^2.0.0, clean-stack@^2.1.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b"
integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==
Expand Down Expand Up @@ -4066,6 +4066,11 @@ electron-localshortcut@^3.1.0:
keyboardevent-from-electron-accelerator "^2.0.0"
keyboardevents-areequal "^0.2.1"

electron-log@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/electron-log/-/electron-log-4.0.2.tgz#159431b60688297420ce233895ec7f72d72e84dc"
integrity sha512-pj5iTMvk2Wi0YtXFX1kM4QU9Dxpg9mTZnvIiFInsFBikxMl1HLs2S+ODQ1yIT4+tLLeBocu0BZdyyshm8/2png==

electron-md-to-pdf@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/electron-md-to-pdf/-/electron-md-to-pdf-2.2.0.tgz#8bc4026252f767180858638c6387246f1148f496"
Expand Down Expand Up @@ -4121,6 +4126,16 @@ electron-to-chromium@^1.3.322:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.322.tgz#a6f7e1c79025c2b05838e8e344f6e89eb83213a8"
integrity sha512-Tc8JQEfGQ1MzfSzI/bTlSr7btJv/FFO7Yh6tanqVmIWOuNCu6/D1MilIEgLtmWqIrsv+o4IjpLAhgMBr/ncNAA==

electron-unhandled@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/electron-unhandled/-/electron-unhandled-3.0.1.tgz#34794326d850ac7bc1a1d2fe90f2b0fcd4d12c9c"
integrity sha512-eIK3LpT/1hrBK4dJjHEQLbTvI8XOflItCOtMzY2RIaRfGAXV0JfJoTe+Y59Q9b/87oXWmSD5p2Dnp2Nzgtlijg==
dependencies:
clean-stack "^2.1.0"
electron-is-dev "^1.0.1"
ensure-error "^2.0.0"
lodash.debounce "^4.0.8"

electron-updater@^4.0.14:
version "4.2.0"
resolved "https://registry.yarnpkg.com/electron-updater/-/electron-updater-4.2.0.tgz#f9ecfc657f65ead737d42b9efecf628d3756b550"
Expand Down Expand Up @@ -4222,6 +4237,11 @@ enhanced-resolve@^4.1.0:
memory-fs "^0.5.0"
tapable "^1.0.0"

ensure-error@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ensure-error/-/ensure-error-2.0.0.tgz#b8359a992601601b3541af9472f6a49d9dca1458"
integrity sha512-1ela4oR5A+TdtFpfiQrZKFUbsOi4JuIYmz2qSGFar6pEdRa54E15mKHVVYrAq1OQhd6b6nVrCaQxQlo6kYwhaw==

entities@^1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
Expand Down

0 comments on commit 2e06520

Please sign in to comment.