Skip to content

Commit c9ae873

Browse files
committed
fix(index.ts): Implemented Duplicate Service Regn. Attempt and Hub Connection Rejection Handling
- Added proper error handling for hub connection rejection - Implemented graceful process termination on duplicate detection - Enhanced error logging with detailed service context - Added connection_error event handler for hub rejection - Implemented custom error type handling with error codes - Added automatic process exit on DUPLICATE_SERVICE_REGISTRATION error This ensures proper client-side handling of hub rejections for duplicate services, providing clear error messaging and preventing service conflicts through automatic termination. Works in conjunction with hub-side validation for complete security implementation. fix #3
1 parent 4834782 commit c9ae873

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { io, Socket } from "socket.io-client";
22
import { nanoid } from "nanoid";
33
import Logger from "./utils/logger"; // Import the logger
4+
import { exit } from "process";
45

56
/**
67
* Options for initializing the MicrostreamClient.
@@ -128,6 +129,38 @@ export class MicrostreamClient {
128129
);
129130
}
130131
});
132+
133+
// Handle socket connection rejection error by the hub
134+
this.socket.on("connect_error", (error) => {
135+
const customError = error as Error & { data?: any };
136+
137+
// Check if the error contains additional data
138+
if (customError instanceof Error && customError.data) {
139+
// If yes, then it's a custom connection error/rejection from the hub
140+
this.logger.error(
141+
`[${this.serviceName}] Connection error to MicroStream Hub: ${customError.data?.code}:`,
142+
customError.data?.content
143+
);
144+
145+
// Special cases for handling specific error codes
146+
switch (customError.data?.code) {
147+
case "DUPLICATE_SERVICE_REGISTRATION":
148+
// Exit the process if a duplicate service registration attempt is detected
149+
exit(1);
150+
break;
151+
152+
// Add more cases as needed in the future
153+
154+
default:
155+
// Do something on default or continue as usual
156+
break;
157+
}
158+
} else {
159+
this.logger.error(
160+
`[${this.serviceName}] Connection error to Microstream Hub: ${customError.message}`
161+
);
162+
}
163+
});
131164
}
132165

133166
/**

0 commit comments

Comments
 (0)