From cc3d0252c9b428d576407d9e5e58f53a0d05406d Mon Sep 17 00:00:00 2001 From: Molly Draven Date: Fri, 27 Sep 2024 21:44:56 -0400 Subject: [PATCH] refactor: migrate socketErrorHandler to separate module --- packages/gateway/src/index.ts | 45 +------------------ .../gateway/src/socketErrorHandler.test.ts | 38 ++++++++++++++++ packages/gateway/src/socketErrorHandler.ts | 30 +++++++++++++ 3 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 packages/gateway/src/socketErrorHandler.test.ts create mode 100644 packages/gateway/src/socketErrorHandler.ts diff --git a/packages/gateway/src/index.ts b/packages/gateway/src/index.ts index bf2790381..a999b0809 100644 --- a/packages/gateway/src/index.ts +++ b/packages/gateway/src/index.ts @@ -29,49 +29,7 @@ import { getGatewayServer } from "./GatewayServer.js"; import { getPortMessageType, UserStatusManager } from "rusty-motors-nps"; import { BasePacket } from "rusty-motors-shared-packets"; import * as Sentry from "@sentry/node"; - -/** - * @typedef {object} OnDataHandlerArgs - * @property {object} args - * @property {string} args.connectionId The connection id of the socket that - * received the data. - * @property {module:packages/shared/RawMessage} args.message The data that was received. - * @property {module:shared/log.ServerLogger} [args.log=getServerLogger({ name: "gateway" })] The logger to use. - * response - * to the - * data. - */ - -/** - * @typedef {function} OnDataHandler - * @param {OnDataHandlerArgs} args The arguments for the handler. - * @returns {ServiceResponse} The - * response - * to the - * data. - */ - -/** - * Handle socket errors - */ -export function socketErrorHandler({ - connectionId, - error, - log = getServerLogger({ - name: "socketErrorHandler", - }), -}: { - connectionId: string; - error: NodeJS.ErrnoException; - log?: ServerLogger; -}) { - // Handle socket errors - if (error.code == "ECONNRESET") { - log.debug(`Connection ${connectionId} reset`); - return; - } - throw Error(`Socket error: ${error.message} on connection ${connectionId}`); -} +import { socketErrorHandler } from "./socketErrorHandler.js"; /** * Handle the end of a socket connection @@ -179,7 +137,6 @@ export function onSocketConnection({ incomingSocket.on( "data", function socketDataHandler(incomingDataAsBuffer: Buffer) { - log.trace( `Incoming data on port ${localPort}: ${incomingDataAsBuffer.toString( "hex", diff --git a/packages/gateway/src/socketErrorHandler.test.ts b/packages/gateway/src/socketErrorHandler.test.ts new file mode 100644 index 000000000..98c905e50 --- /dev/null +++ b/packages/gateway/src/socketErrorHandler.test.ts @@ -0,0 +1,38 @@ +import { describe, it, expect, vi } from "vitest"; +import { socketErrorHandler } from "./socketErrorHandler.js"; +import { type ServerLogger } from "rusty-motors-shared"; + +describe("socketErrorHandler", () => { + + it("should log a debug message when error code is ECONNRESET", () => { + const connectionId = "12345"; + const error = { code: "ECONNRESET" } as NodeJS.ErrnoException; + const mockLogger = { + debug: vi.fn(), + } as unknown as ServerLogger; + + + socketErrorHandler({ connectionId, error, log: mockLogger }); + + expect(mockLogger.debug).toHaveBeenCalledWith( + `Connection ${connectionId} reset`, + ); + }); + + it("should throw an error when error code is not handled", () => { + const connectionId = "12345"; + const error = { + code: "EUNKNOWN", + message: "Unknown error", + } as NodeJS.ErrnoException; + const mockLogger = { + debug: vi.fn(), + } as unknown as ServerLogger; + + + expect(() => + socketErrorHandler({ connectionId, error, log: mockLogger }), + ).toThrow(`Socket error: ${error.message} on connection ${connectionId}`); + }); + +}); diff --git a/packages/gateway/src/socketErrorHandler.ts b/packages/gateway/src/socketErrorHandler.ts new file mode 100644 index 000000000..12df58624 --- /dev/null +++ b/packages/gateway/src/socketErrorHandler.ts @@ -0,0 +1,30 @@ +import { getServerLogger, type ServerLogger } from "rusty-motors-shared"; + +/** + * Handles socket errors by logging specific error codes or throwing an error. + * + * @param {Object} params - The parameters for the socket error handler. + * @param {string} params.connectionId - The ID of the connection where the error occurred. + * @param {NodeJS.ErrnoException} params.error - The error object containing details of the socket error. + * @param {ServerLogger} [params.log] - Optional logger instance for logging error details. Defaults to a server logger named "socketErrorHandler". + * + * @throws {Error} Throws an error if the socket error code is not handled. + */ +export function socketErrorHandler({ + connectionId, + error, + log = getServerLogger({ + name: "socketErrorHandler", + }), +}: { + connectionId: string; + error: NodeJS.ErrnoException; + log?: ServerLogger; +}) { + // Handle socket errors + if (error.code == "ECONNRESET") { + log.debug(`Connection ${connectionId} reset`); + return; + } + throw Error(`Socket error: ${error.message} on connection ${connectionId}`); +}