From da3f6c0164f07d2e1e65ddd1937ce425f6d5bd0f Mon Sep 17 00:00:00 2001 From: Ronan Fegan Date: Tue, 5 Jul 2022 10:40:26 -0700 Subject: [PATCH] fix(microservice): RMQ health check always healthy even when down RMQ throws an error which is not instance of `Error`. Because of that it did not throw a `HealthCheckError` which results in a healthy response. resolves #1891 --- .../microservice.health.e2e-spec.ts | 28 ++++++++++++++++++- .../microservice/microservice.health.ts | 7 +++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/e2e/health-checks/microservice.health.e2e-spec.ts b/e2e/health-checks/microservice.health.e2e-spec.ts index 97197b30e..a9d8cf9fb 100644 --- a/e2e/health-checks/microservice.health.e2e-spec.ts +++ b/e2e/health-checks/microservice.health.e2e-spec.ts @@ -1,6 +1,5 @@ import * as request from 'supertest'; import { INestApplication, INestMicroservice } from '@nestjs/common'; - import { bootstrapMicroservice, bootstrapTestingModule, @@ -71,6 +70,33 @@ describe('MicroserviceHealthIndicator', () => { }); }); + it('should throw an error if an RMQ microservice is not reachable', async () => { + app = await setHealthEndpoint(({ healthCheck, microservice }) => + healthCheck.check([ + async () => + microservice.pingCheck('rmq', { + transport: Transport.RMQ, + options: { + host: '0.0.0.0', + port: 8889, + }, + }), + ]), + ).start(); + + await microservice.close(); + + const details = { + rmq: { status: 'down', message: 'rmq is not available' }, + }; + return request(app.getHttpServer()).get('/health').expect(503).expect({ + status: 'error', + info: {}, + error: details, + details, + }); + }); + afterEach(async () => await app.close()); afterEach(async () => await microservice.close()); }); diff --git a/lib/health-indicator/microservice/microservice.health.ts b/lib/health-indicator/microservice/microservice.health.ts index 26f0239d1..18ceed6ed 100644 --- a/lib/health-indicator/microservice/microservice.health.ts +++ b/lib/health-indicator/microservice/microservice.health.ts @@ -129,6 +129,13 @@ export class MicroserviceHealthIndicator extends HealthIndicator { if (isError(err)) { this.generateError(key, err, timeout); } + + const errorMsg = `${key} is not available`; + + throw new HealthCheckError( + errorMsg, + this.getStatus(key, false, { message: errorMsg }), + ); } return this.getStatus(key, isHealthy);