Skip to content

Commit 337fdf0

Browse files
authored
feat: global NATS interceptor implementation (#1091)
* global interceptor implementation for nats Signed-off-by: Tipu_Singh <[email protected]> * added type in exception Signed-off-by: Tipu_Singh <[email protected]> --------- Signed-off-by: Tipu_Singh <[email protected]>
1 parent 65dd612 commit 337fdf0

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

apps/api-gateway/common/exception-handler.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
2424
}
2525
if (exception instanceof HttpException) {
2626
status = exception.getStatus();
27+
2728
}
2829

29-
let exceptionResponse: ExceptionResponse;
30+
let exceptionResponse: ExceptionResponse = {} as ExceptionResponse;
31+
const exceptionResponseData = exception.getResponse ? exception.getResponse() : exception;
3032

31-
if (exception['response']) {
32-
exceptionResponse = exception['response'];
33+
if ('string' === typeof exceptionResponseData) {
34+
exceptionResponse.message = exceptionResponseData;
3335
} else {
34-
exceptionResponse = exception as unknown as ExceptionResponse;
36+
exceptionResponse = exceptionResponseData as unknown as ExceptionResponse;
3537
}
3638

39+
if (exceptionResponse.message && exceptionResponse.message.includes(ResponseMessages.nats.error.noSubscribers)) {
40+
exceptionResponse.message = ResponseMessages.nats.error.noSubscribers;
41+
}
3742
errorResponse = {
3843
statusCode: exceptionResponse.statusCode ? exceptionResponse.statusCode : status,
3944
message: exceptionResponse.message
@@ -43,7 +48,6 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
4348
? exceptionResponse.error
4449
: ResponseMessages.errorMessages.serverError
4550
};
46-
4751
response.status(errorResponse.statusCode).json(errorResponse);
4852
}
4953
}

apps/api-gateway/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getNatsOptions } from '@credebl/common/nats.config';
1313
import helmet from 'helmet';
1414
import { CommonConstants } from '@credebl/common/common.constant';
1515
import NestjsLoggerServiceAdapter from '@credebl/logger/nestjsLoggerServiceAdapter';
16+
import { NatsInterceptor } from '../../../libs/interceptors/nats.interceptor';
1617
dotenv.config();
1718

1819
async function bootstrap(): Promise<void> {
@@ -95,6 +96,7 @@ async function bootstrap(): Promise<void> {
9596
xssFilter: true
9697
})
9798
);
99+
app.useGlobalInterceptors(new NatsInterceptor());
98100
await app.listen(process.env.API_GATEWAY_PORT, `${process.env.API_GATEWAY_HOST}`);
99101
Logger.log(`API Gateway is listening on port ${process.env.API_GATEWAY_PORT}`);
100102
}

libs/common/src/response-messages/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,5 +475,14 @@ export const ResponseMessages = {
475475
notFoundBaseWallet: 'The base wallet record is missing.',
476476
walletRecordNotFound: 'Wallet record not found.'
477477
}
478+
},
479+
nats: {
480+
success: {
481+
482+
},
483+
error: {
484+
noSubscribers: 'No subscribers for the requested message. Error while connecting to NATS, service might not be started',
485+
natsConnect: 'Empty response. There are no subscribers listening to that message'
486+
}
478487
}
479488
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ResponseMessages } from '@credebl/common/response-messages';
2+
import {
3+
CallHandler,
4+
ExecutionContext,
5+
Injectable,
6+
NestInterceptor,
7+
HttpException,
8+
Logger
9+
} from '@nestjs/common';
10+
import { Observable, throwError } from 'rxjs';
11+
import { catchError } from 'rxjs/operators';
12+
13+
@Injectable()
14+
export class NatsInterceptor implements NestInterceptor {
15+
private readonly logger = new Logger(NatsInterceptor.name);
16+
17+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
18+
return next.handle().pipe(
19+
catchError((error) => {
20+
if (error.message.includes(ResponseMessages.nats.error.natsConnect)) {
21+
this.logger.error(`No subscribers for message: ${error.message}`);
22+
return throwError(() => new HttpException(ResponseMessages.nats.error.noSubscribers, 500));
23+
}
24+
return throwError(() => error);
25+
})
26+
);
27+
}
28+
}

0 commit comments

Comments
 (0)