From d81e788c7df7791dd31d2f3151e8deb9c394ca82 Mon Sep 17 00:00:00 2001 From: Sean Boult <996134+Hacksore@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:23:57 -0500 Subject: [PATCH] Add moar logging --- apps/desktop/package.json | 1 + apps/desktop/src/rpc/event.ts | 1 + apps/desktop/src/rpc/manager.ts | 42 +++++++++++++++++++++++---------- pnpm-lock.yaml | 11 +++++++++ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/apps/desktop/package.json b/apps/desktop/package.json index c1465116..4b2a0293 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -46,6 +46,7 @@ "tailwindcss-animate": "^1.0.7", "tauri-plugin-websocket-api": "github:tauri-apps/tauri-plugin-websocket#v1", "tauri-plugin-window-state-api": "github:tauri-apps/tauri-plugin-window-state#v1", + "tauri-plugin-log": "github:tauri-apps/tauri-plugin-log#v1", "uuid": "^9.0.1", "zustand": "^4.5.2" }, diff --git a/apps/desktop/src/rpc/event.ts b/apps/desktop/src/rpc/event.ts index f8bcb9fa..261c7699 100644 --- a/apps/desktop/src/rpc/event.ts +++ b/apps/desktop/src/rpc/event.ts @@ -2,6 +2,7 @@ export enum RPCEvent { /** non-subscription event sent immediately after connecting, contains server information */ READY = "READY", /** non-subscription event sent when there is an error, including command responses */ + ERROR = "ERROR", /** sent when a user in a subscribed voice channel speaks */ SPEAKING_START = "SPEAKING_START", diff --git a/apps/desktop/src/rpc/manager.ts b/apps/desktop/src/rpc/manager.ts index 026e4a00..8f2cb7dd 100644 --- a/apps/desktop/src/rpc/manager.ts +++ b/apps/desktop/src/rpc/manager.ts @@ -17,6 +17,7 @@ import type { VoiceUser } from "@/types"; import { getVersion } from "@tauri-apps/api/app"; import { hash } from "@/utils/crypto"; import { invoke } from "@tauri-apps/api"; +import { error, info, debug } from "tauri-plugin-log"; interface TokenResponse { access_token: string; @@ -101,7 +102,7 @@ class SocketManager { * Setup the websocket connection and listen for messages */ async init(navigate: NavigateFunction) { - console.log("Init web socket manager"); + info("[WEBSOCKET] Init manager"); this.disconnect(); this._navigate = navigate; @@ -135,6 +136,7 @@ class SocketManager { } public disconnect() { + debug("[WEBSOCKET] Disconnecting socket"); this.socket?.disconnect(); this.isConnected = false; } @@ -180,6 +182,7 @@ class SocketManager { // TODO: this has to be a bug in the upstream lib, we should get a proper code // and not have to check the raw dawg string to see if something is wrong if (typeof event === "string" && (event as string).includes("Connection reset without closing handshake")) { + error("[WEBSOCKET] Connection reset without closing handshake"); this.navigate("/error"); await this.unpin(); @@ -199,8 +202,10 @@ class SocketManager { const acessToken = this.userdataStore.accessToken; if (acessToken) { + debug("[WEBSOCKET] Found access token and attempting to login with it"); this.login(acessToken); } else { + debug("[WEBSOCKET] No access token found attempting to login"); this.authenticate(); } } @@ -271,17 +276,26 @@ class SocketManager { // we got a token back from discord let's fetch an access token if (payload.cmd === RPCCommand.AUTHORIZE) { + debug("[WEBSOCKET] Got valid code back from discord, fetching access token..."); const { code } = payload.data; - const res = await fetch(`${API_URL}/token`, { - method: "POST", - body: Body.json({ code }), - }); - // we need send the token to discord - this.userdataStore.setAccessToken(res.data.access_token); + try { + const res = await fetch(`${API_URL}/token`, { + method: "POST", + body: Body.json({ code }), + }); + + debug("[WEBSOCKET] Obtained access token api, attempting to login with it..."); - // login with the token - this.login(res.data.access_token); + // we need send the token to discord + this.userdataStore.setAccessToken(res.data.access_token); + + // login with the token + this.login(res.data.access_token); + } catch (e) { + error(`[WEBSOCKET] Error fetching token: ${JSON.stringify(e)}`); + this.navigate("/error"); + } } // GET_SELECTED_VOICE_CHANNEL used to get the current voice channel the client is in @@ -297,9 +311,9 @@ class SocketManager { } } - // console.log(payload); - // we are ready to do things cause we are fully authed + // we hit an auth error with discord if (payload.cmd === RPCCommand.AUTHENTICATE && payload.evt === RPCEvent.ERROR) { + error(`[WEBSOCKET] Error authenticating with discord: ${JSON.stringify(payload.data)}`); // they have a token from the old client id if (payload.data.code === RPCErrors.INVALID_CLIENTID) { this.userdataStore.removeAccessToken(); @@ -318,6 +332,7 @@ class SocketManager { // track error metric track(MetricNames.DiscordAuthed, 0); } else if (payload?.cmd === RPCCommand.AUTHENTICATE) { + info("[WEBSOCKET] Logged into discord successfully"); // track success metric track(MetricNames.DiscordAuthed, 1); @@ -357,6 +372,7 @@ class SocketManager { // when we move channels we get a new list of users if (payload.cmd === RPCCommand.GET_CHANNEL) { + debug("[WEBSOCKET] Requesting channel for user"); this.requestUserChannel(); } } @@ -414,12 +430,12 @@ export const useSocket = () => { return; } + // if the socket is already initialized return if (socketRef.current) { - console.log("Socket already initialized"); return; } - console.log("Initializing websocket..."); + info("[WEBSOCKET] Initializing websocket..."); const socketManager = new SocketManager(); socketManager.init(navigate); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc145786..15b7e4cb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -187,6 +187,9 @@ importers: tailwindcss-animate: specifier: ^1.0.7 version: 1.0.7(tailwindcss@3.4.4) + tauri-plugin-log: + specifier: github:tauri-apps/tauri-plugin-log#v1 + version: tauri-plugin-log-api@https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/2bb26e22f7f7b4f164bad02f0ae4085796f77fff tauri-plugin-websocket-api: specifier: github:tauri-apps/tauri-plugin-websocket#v1 version: https://codeload.github.com/tauri-apps/tauri-plugin-websocket/tar.gz/c23ba8b03953e459fd9a56ea6113b12461b78026 @@ -5187,6 +5190,10 @@ packages: resolution: {integrity: sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==} engines: {node: '>=10'} + tauri-plugin-log-api@https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/2bb26e22f7f7b4f164bad02f0ae4085796f77fff: + resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/2bb26e22f7f7b4f164bad02f0ae4085796f77fff} + version: 0.0.0 + tauri-plugin-websocket-api@https://codeload.github.com/tauri-apps/tauri-plugin-websocket/tar.gz/c23ba8b03953e459fd9a56ea6113b12461b78026: resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-websocket/tar.gz/c23ba8b03953e459fd9a56ea6113b12461b78026} version: 0.0.0 @@ -11657,6 +11664,10 @@ snapshots: mkdirp: 1.0.4 yallist: 4.0.0 + tauri-plugin-log-api@https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/2bb26e22f7f7b4f164bad02f0ae4085796f77fff: + dependencies: + '@tauri-apps/api': 1.6.0 + tauri-plugin-websocket-api@https://codeload.github.com/tauri-apps/tauri-plugin-websocket/tar.gz/c23ba8b03953e459fd9a56ea6113b12461b78026: dependencies: '@tauri-apps/api': 1.6.0