-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 🎨 migrate to socket module (#2180)
migrate from using the state feature of the shared module to the socket module <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new socket management system for improved connection handling. - Added structured error handling for socket connections. - **Bug Fixes** - Updated logging to accurately reflect the new socket IDs. - **Refactor** - Removed outdated socket handling functions and properties to streamline state management. - **Chores** - Added a new dependency for socket functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
8 changed files
with
110 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.