Skip to content

Commit

Permalink
fix(microservice): RMQ health check always healthy even when down
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ronan-spinwheel authored and BrunnerLivio committed Jul 5, 2022
1 parent a170bb7 commit da3f6c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 27 additions & 1 deletion e2e/health-checks/microservice.health.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as request from 'supertest';
import { INestApplication, INestMicroservice } from '@nestjs/common';

import {
bootstrapMicroservice,
bootstrapTestingModule,
Expand Down Expand Up @@ -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());
});
7 changes: 7 additions & 0 deletions lib/health-indicator/microservice/microservice.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit da3f6c0

Please sign in to comment.