Skip to content

Commit

Permalink
Merge pull request #46 from wakatime/feature/error-handling
Browse files Browse the repository at this point in the history
Report unhandled errors to WakaTime API
  • Loading branch information
alanhamlett authored Oct 17, 2024
2 parents 772b726 + 74f0c92 commit 32809c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
49 changes: 40 additions & 9 deletions electron/helpers/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import fs, { createWriteStream } from "node:fs";
import path from "node:path";
import { pipeline } from "node:stream";
import { promisify } from "node:util";
import { LogLevel, Logging } from "../utils/logging";
import {
addHours,
formatDistanceToNow,
Expand All @@ -10,10 +7,6 @@ import {
intervalToDuration,
isBefore,
} from "date-fns";
import fetch from "node-fetch";
import unzpier from "unzipper";
import { z } from "zod";

import {
exec,
getArch,
Expand All @@ -22,8 +15,15 @@ import {
getResourcesFolderPath,
parseJSONObject,
} from "../utils";
import { Logging, LogLevel } from "../utils/logging";
import fs, { createWriteStream } from "node:fs";

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 @@ -255,4 +255,35 @@ export abstract class Dependencies {
throw new Error(`${cli} file not found!`);
}
}

static async reportError(
error: Error,
origin: string,
versionString: string,
) {
const url = "https://api.wakatime.com/api/v1/errors/javascript";
const apiKey = ConfigFile.getSetting("settings", "api_key");
const headers: Record<string, string> = {
"Content-Type": "application/json",
"User-Agent": `desktop-wakatime ${versionString}`,
};
if (apiKey) {
headers["Authorization"] = `Basic ${apiKey}`;
}
const resp = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify({
browser_url: `desktop-wakatime://${versionString}`,
script_url: origin,
message: error.message,
traceback: error.stack,
line: (error as unknown as { lineNumber: number }).lineNumber,
column: (error as unknown as { columnNumber: number }).columnNumber,
}),
});
if (!resp.ok) {
console.log(await resp.text());
}
}
}
22 changes: 14 additions & 8 deletions electron/watchers/wakatime.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { WindowInfo } from "@miniben90/x-win";
import { app, Notification, shell, Tray } from "electron";
import { updateElectronApp } from "update-electron-app";

import type { Category, EntityType } from "../utils/types";
import { LogLevel, Logging } from "../utils/logging";
import { Notification, Tray, app, shell } from "electron";
import { exec, getCLIPath, getDeepLinkUrl, getPlatfrom } from "../utils";

import type { AppData } from "../utils/validators";
import { AppsManager } from "../helpers/apps-manager";
import { ConfigFile } from "../helpers/config-file";
import { DeepLink } from "../utils/constants";
import { Dependencies } from "../helpers/dependencies";
import { MonitoringManager } from "../helpers/monitoring-manager";
import { PropertiesManager } from "../helpers/properties-manager";
import { SettingsManager } from "../helpers/settings-manager";
import { exec, getCLIPath, getDeepLinkUrl, getPlatfrom } from "../utils";
import { DeepLink } from "../utils/constants";
import { Logging, LogLevel } from "../utils/logging";
import { WindowInfo } from "@miniben90/x-win";
import { updateElectronApp } from "update-electron-app";

export class Wakatime {
private lastEntitiy = "";
Expand All @@ -24,7 +24,12 @@ export class Wakatime {
private versionString: string;

constructor() {
this.versionString = `${getPlatfrom()}-wakatime/${app.getVersion()}`;
const version = `${getPlatfrom()}-wakatime/${app.getVersion()}`;
this.versionString = version;
process.on("uncaughtException", function (error, origin) {
void Dependencies.reportError(error, origin, version);
console.log(error);
});
}

init(tray: Tray | null) {
Expand All @@ -40,6 +45,7 @@ export class Wakatime {
info: (message) => Logging.instance().log(message, LogLevel.INFO),
warn: (message) => Logging.instance().log(message, LogLevel.WARN),
},
updateInterval: "1 hour",
});
}

Expand Down
8 changes: 4 additions & 4 deletions electron/watchers/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { IGlobalKeyListener } from "node-global-key-listener";
import {
WindowInfo,
activeWindow,
subscribeActiveWindow,
unsubscribeActiveWindow,
WindowInfo,
} from "@miniben90/x-win";
import { GlobalKeyboardListener } from "node-global-key-listener";

import { AppsManager } from "../helpers/apps-manager";
import { GlobalKeyboardListener } from "node-global-key-listener";
import type { IGlobalKeyListener } from "node-global-key-listener";
import { Logging } from "../utils/logging";
import { MonitoredApp } from "../helpers/monitored-app";
import { MonitoringManager } from "../helpers/monitoring-manager";
import { Logging } from "../utils/logging";
import { Wakatime } from "./wakatime";

export class Watcher {
Expand Down

0 comments on commit 32809c8

Please sign in to comment.