From 6ea47debe1a53d471bab7244c379124f53f0a3c0 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Tue, 12 Nov 2024 20:49:53 +0100 Subject: [PATCH] Improve error handling, improve tray click handling --- electron/helpers/dependencies.ts | 20 ++++++++++---------- electron/main.ts | 22 +++++++++++++++++----- electron/utils/logging.ts | 16 +++++++++++++++- electron/watchers/wakatime.ts | 6 +++--- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/electron/helpers/dependencies.ts b/electron/helpers/dependencies.ts index bce8642..0df2c0f 100644 --- a/electron/helpers/dependencies.ts +++ b/electron/helpers/dependencies.ts @@ -1,4 +1,7 @@ -import { LogLevel, Logging } from "../utils/logging"; +import fs, { createWriteStream } from "node:fs"; +import path from "node:path"; +import { pipeline } from "node:stream"; +import { promisify } from "node:util"; import { addHours, formatDistanceToNow, @@ -7,6 +10,10 @@ import { intervalToDuration, isBefore, } from "date-fns"; +import fetch from "node-fetch"; +import unzpier from "unzipper"; +import { z } from "zod"; + import { exec, getArch, @@ -15,15 +22,8 @@ import { getResourcesFolderPath, parseJSONObject, } from "../utils"; -import fs, { createWriteStream } from "node:fs"; - +import { Logging, LogLevel } from "../utils/logging"; import { ConfigFile } from "./config-file"; -import fetch from "node-fetch"; -import path from "node:path"; -import { pipeline } from "node:stream"; -import { promisify } from "node:util"; -import unzpier from "unzipper"; -import { z } from "zod"; const streamPipeline = promisify(pipeline); @@ -283,7 +283,7 @@ export abstract class Dependencies { }), }); if (!resp.ok) { - console.log(await resp.text()); + Logging.instance().log(await resp.text(), LogLevel.WARN); } } } diff --git a/electron/main.ts b/electron/main.ts index ebffa43..d7f51f5 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -19,7 +19,7 @@ import { PropertiesManager } from "./helpers/properties-manager"; import { SettingsManager } from "./helpers/settings-manager"; import { getLogFilePath } from "./utils"; import { DeepLink, IpcKeys, WAKATIME_PROTOCALL } from "./utils/constants"; -import { Logging } from "./utils/logging"; +import { Logging, LogLevel } from "./utils/logging"; import { AppData } from "./utils/validators"; import { Wakatime } from "./watchers/wakatime"; import { Watcher } from "./watchers/watcher"; @@ -226,10 +226,22 @@ function createTray() { ]); tray.setToolTip("WakaTime"); tray.setContextMenu(contextMenu); - tray.addListener("click", () => { - tray?.popUpContextMenu(); - wakatime?.fetchToday(); - }); + + const handleClick = () => { + if (!tray) { + Logging.instance().log("Tray is not initialized", LogLevel.ERROR); + return; + } + try { + tray.popUpContextMenu(); + wakatime?.fetchToday(); + } catch (error) { + Logging.instance().log(`Tray click error: ${error}`); + } + }; + tray.addListener("click", handleClick); + tray.addListener("right-click", handleClick); + tray.addListener("double-click", handleClick); } // Hide app from macOS doc diff --git a/electron/utils/logging.ts b/electron/utils/logging.ts index 44b5b61..79cfe17 100644 --- a/electron/utils/logging.ts +++ b/electron/utils/logging.ts @@ -1,6 +1,8 @@ import fs from "node:fs"; import path from "node:path"; import { format } from "date-fns"; +import { app } from "electron"; +import { Dependencies } from "electron/helpers/dependencies"; import { getResourcesFolderPath } from "."; @@ -44,7 +46,7 @@ export class Logging { this.filePath = null; } - public log(msg: string, level = LogLevel.DEBUG) { + public log(msg: string, level = LogLevel.DEBUG, skipDiagnostics = false) { if (level < this.level) { return; } @@ -67,5 +69,17 @@ export class Logging { fs.writeFileSync(this.filePath, logMessage, { encoding: "utf-8" }); } } + + if (!skipDiagnostics && level == LogLevel.ERROR) { + try { + throw new Error(msg); + } catch (e) { + void Dependencies.reportError( + e as Error, + "console.error", + app.getVersion(), + ); + } + } } } diff --git a/electron/watchers/wakatime.ts b/electron/watchers/wakatime.ts index 390f38f..3632332 100644 --- a/electron/watchers/wakatime.ts +++ b/electron/watchers/wakatime.ts @@ -28,9 +28,9 @@ export class Wakatime { constructor() { const version = `${getPlatfrom()}-wakatime/${app.getVersion()}`; this.versionString = version; - process.on("uncaughtException", function (error, origin) { - void Dependencies.reportError(error, origin, version); - console.log(error); + process.on("uncaughtException", async function (error, origin) { + await Dependencies.reportError(error, origin, version); + Logging.instance().log(error.toString(), LogLevel.ERROR, true); }); }