diff --git a/app/components/WebsocketProvider.tsx b/app/components/WebsocketProvider.tsx index 18cd7b0abe..bf56929990 100644 --- a/app/components/WebsocketProvider.tsx +++ b/app/components/WebsocketProvider.tsx @@ -13,7 +13,7 @@ import { FileOperationType, ImportState, } from "@shared/types"; -import { toError } from "@shared/utils/error"; +import { errToId, toError } from "@shared/utils/error"; import type RootStore from "~/stores/RootStore"; import type Collection from "~/models/Collection"; import type Comment from "~/models/Comment"; @@ -47,6 +47,12 @@ type SocketWithAuthentication = Socket & { authenticated?: boolean; }; +/** Identifiers of socket authentication errors that are expected in normal use. */ +const unreportableErrorIds = [ + "authentication_required", + "invalid_authentication", +]; + export const WebsocketContext = createContext( null ); @@ -138,7 +144,10 @@ function useConnectionHandlers() { toast.error(message); - if (message === "No access token") { + // Authentication failures are an expected result of an expired or + // otherwise unusable token, there is nothing to report. + const errorId = errToId(err); + if (errorId && unreportableErrorIds.includes(errorId)) { return; } diff --git a/server/services/websockets.ts b/server/services/websockets.ts index b78e646aed..31d0f1d9f7 100644 --- a/server/services/websockets.ts +++ b/server/services/websockets.ts @@ -5,7 +5,7 @@ import cookie from "cookie"; import type Koa from "koa"; import IO from "socket.io"; import { createAdapter } from "socket.io-redis"; -import { errToString, toError } from "@shared/utils/error"; +import { errToId, errToString, toError } from "@shared/utils/error"; import env from "@server/env"; import { AuthenticationError } from "@server/errors"; import Logger from "@server/logging/Logger"; @@ -156,7 +156,7 @@ export default function init( Logger.debug("websockets", `Authentication error socket ${socket.id}`, { error: message, }); - socket.emit("unauthorized", { message }, function () { + socket.emit("unauthorized", { message, id: errToId(err) }, function () { socket.disconnect(); }); }