Skip to content

Commit

Permalink
Merge pull request #73 from wakatime/bugfix/improve-error-handling
Browse files Browse the repository at this point in the history
Improve error handling, improve tray click handling
  • Loading branch information
alanhamlett authored Nov 12, 2024
2 parents 2f4a577 + 6ea47de commit 25fc622
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
20 changes: 10 additions & 10 deletions electron/helpers/dependencies.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -283,7 +283,7 @@ export abstract class Dependencies {
}),
});
if (!resp.ok) {
console.log(await resp.text());
Logging.instance().log(await resp.text(), LogLevel.WARN);
}
}
}
22 changes: 17 additions & 5 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion electron/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -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 ".";

Expand Down Expand Up @@ -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;
}
Expand All @@ -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(),
);
}
}
}
}
6 changes: 3 additions & 3 deletions electron/watchers/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 25fc622

Please sign in to comment.