Skip to content

Commit 9dd698c

Browse files
Merge pull request #15598 from ghyghoo8/patch-1
feat(errors): add InvalidTcpDataReceptionException for handling TCP receive errors​
2 parents 42afba7 + 68adf92 commit 9dd698c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception';
2+
3+
export class InvalidTcpDataReceptionException extends RuntimeException {
4+
constructor(err: string | Error) {
5+
const errMsgStr =
6+
typeof err === 'string'
7+
? err
8+
: err &&
9+
typeof err === 'object' &&
10+
'message' in err &&
11+
typeof (err as any).message === 'string'
12+
? (err as any).message
13+
: String(err);
14+
const _errMsg = errMsgStr.includes('Corrupted length value')
15+
? `Corrupted length value of the received data supplied in a packet`
16+
: `The invalid received message from tcp server`;
17+
super(_errMsg);
18+
}
19+
}

packages/microservices/server/server-tcp.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TcpContext } from '../ctx-host/tcp.context';
1414
import { Transport } from '../enums';
1515
import { TcpEvents, TcpEventsMap, TcpStatus } from '../events/tcp.events';
1616
import { JsonSocket, TcpSocket } from '../helpers';
17+
import { InvalidTcpDataReceptionException } from '../errors/invalid-tcp-data-reception.exception';
1718
import {
1819
IncomingRequest,
1920
PacketId,
@@ -81,7 +82,10 @@ export class ServerTCP extends Server<TcpEvents, TcpStatus> {
8182
readSocket.on('message', async (msg: ReadPacket & PacketId) =>
8283
this.handleMessage(readSocket, msg),
8384
);
84-
readSocket.on(TcpEventsMap.ERROR, this.handleError.bind(this));
85+
readSocket.on(TcpEventsMap.ERROR, err => {
86+
const invalidError = new InvalidTcpDataReceptionException(err);
87+
this.handleError(invalidError as any);
88+
});
8589
}
8690

8791
public async handleMessage(socket: TcpSocket, rawMessage: unknown) {

0 commit comments

Comments
 (0)