Skip to content

Commit

Permalink
fix(http): responseCheck callback does not get executed on unhealthy …
Browse files Browse the repository at this point in the history
…http res

resolves #1944
  • Loading branch information
BrunnerLivio committed Jul 30, 2022
1 parent 44ca587 commit d9147d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
24 changes: 24 additions & 0 deletions e2e/health-checks/http.health.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ describe(`HttpHealthIndicator`, () => {

return request(app.getHttpServer()).get('/health').expect(503);
});

it('should be healthy if remote server returns 400 and status code 400 is expected', async () => {
await remoteServer.get('/', (_, res) => res.sendStatus(400)).start();
app = await setHealthEndpoint(({ healthCheck, http }) =>
healthCheck.check([
() =>
http.responseCheck(
'http',
remoteServer.url,
(res) => res.status === 400,
),
]),
).start();

return request(app.getHttpServer())
.get('/health')
.expect(200)
.expect({
status: 'ok',
info: { http: { status: 'up' } },
error: {},
details: { http: { status: 'up' } },
});
});
});

afterEach(async () => await app.close());
Expand Down
49 changes: 22 additions & 27 deletions lib/health-indicator/http/http.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { HealthIndicator, HealthIndicatorResult } from '..';
import { HealthCheckError } from '../../health-check/health-check.error';
import { lastValueFrom, Observable } from 'rxjs';
import { ModuleRef } from '@nestjs/core';
import {
checkPackages,
isAxiosError,
isError,
isHealthCheckError,
} from '../../utils';
import { checkPackages, isAxiosError } from '../../utils';
import type * as NestJSAxios from '@nestjs/axios';
import { AxiosRequestConfig, AxiosResponse } from './axios.interfaces';
import { Logger } from '@nestjs/common/services/logger.service';
Expand Down Expand Up @@ -138,35 +133,35 @@ export class HttpHealthIndicator extends HealthIndicator {
): Promise<HealthIndicatorResult> {
const httpService = httpClient || this.getHttpService();

let response: AxiosResponse;
let axiosError: AxiosError | null = null;

try {
const response = await lastValueFrom(
response = await lastValueFrom(
httpService.request({ url: url.toString(), ...options }),
);

const isHealthy = await callback(response);

if (!isHealthy) {
throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false),
);
}

return this.getStatus(key, isHealthy);
} catch (err) {
if (isAxiosError(err)) {
throw this.generateHttpError(key, err);
} catch (error) {
if (isAxiosError(error) && error.response) {
response = error.response;
axiosError = error;
} else {
throw error;
}
}

if (isHealthCheckError(HealthCheckError)) {
throw err;
}
const isHealthy = await callback(response);

if (isError(err)) {
throw new HealthCheckError(err.message, this.getStatus(key, false));
if (!isHealthy) {
if (axiosError) {
throw this.generateHttpError(key, axiosError);
}

throw new HealthCheckError(err as any, this.getStatus(key, false));
throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false),
);
}

return this.getStatus(key, true);
}
}

0 comments on commit d9147d8

Please sign in to comment.