Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: global NATS interceptor implementation #1091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions apps/api-gateway/common/exception-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
}
if (exception instanceof HttpException) {
status = exception.getStatus();

}

let exceptionResponse: ExceptionResponse;
let exceptionResponse: ExceptionResponse = {} as ExceptionResponse;
const exceptionResponseData = exception.getResponse ? exception.getResponse() : exception;

if (exception['response']) {
exceptionResponse = exception['response'];
if ('string' === typeof exceptionResponseData) {
exceptionResponse.message = exceptionResponseData;
} else {
exceptionResponse = exception as unknown as ExceptionResponse;
exceptionResponse = exceptionResponseData as unknown as ExceptionResponse;
}

if (exceptionResponse.message && exceptionResponse.message.includes(ResponseMessages.nats.error.noSubscribers)) {
exceptionResponse.message = ResponseMessages.nats.error.noSubscribers;
}
errorResponse = {
statusCode: exceptionResponse.statusCode ? exceptionResponse.statusCode : status,
message: exceptionResponse.message
Expand All @@ -43,7 +48,6 @@ export class CustomExceptionFilter extends BaseExceptionFilter {
? exceptionResponse.error
: ResponseMessages.errorMessages.serverError
};

response.status(errorResponse.statusCode).json(errorResponse);
}
}
2 changes: 2 additions & 0 deletions apps/api-gateway/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getNatsOptions } from '@credebl/common/nats.config';
import helmet from 'helmet';
import { CommonConstants } from '@credebl/common/common.constant';
import NestjsLoggerServiceAdapter from '@credebl/logger/nestjsLoggerServiceAdapter';
import { NatsInterceptor } from '../../../libs/interceptors/nats.interceptor';
dotenv.config();

async function bootstrap(): Promise<void> {
Expand Down Expand Up @@ -95,6 +96,7 @@ async function bootstrap(): Promise<void> {
xssFilter: true
})
);
app.useGlobalInterceptors(new NatsInterceptor());
await app.listen(process.env.API_GATEWAY_PORT, `${process.env.API_GATEWAY_HOST}`);
Logger.log(`API Gateway is listening on port ${process.env.API_GATEWAY_PORT}`);
}
Expand Down
9 changes: 9 additions & 0 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,5 +465,14 @@ export const ResponseMessages = {
notFoundBaseWallet: 'The base wallet record is missing.',
walletRecordNotFound: 'Wallet record not found.'
}
},
nats: {
success: {

},
error: {
noSubscribers: 'No subscribers for the requested message. Error while connecting to NATS, service might not be started',
natsConnect: 'Empty response. There are no subscribers listening to that message'
}
}
};
28 changes: 28 additions & 0 deletions libs/interceptors/nats.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ResponseMessages } from '@credebl/common/response-messages';
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
HttpException,
Logger
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class NatsInterceptor implements NestInterceptor {
private readonly logger = new Logger(NatsInterceptor.name);

intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(
catchError((error) => {
if (error.message.includes(ResponseMessages.nats.error.natsConnect)) {
this.logger.error(`No subscribers for message: ${error.message}`);
return throwError(() => new HttpException(ResponseMessages.nats.error.noSubscribers, 500));
}
return throwError(() => error);
})
);
}
}