Skip to content

Commit

Permalink
fix: minor code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Mar 29, 2020
1 parent 75afe4f commit 14df600
Show file tree
Hide file tree
Showing 5 changed files with 1,369 additions and 2,080 deletions.
94 changes: 47 additions & 47 deletions lib/health-check/health-check-executor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,6 @@ import {
export class HealthCheckExecutor implements BeforeApplicationShutdown {
private isShuttingDown = false;

private async executeHealthIndicators(
healthIndicators: HealthIndicatorFunction[],
) {
const results: any[] = [];
const errors: any[] = [];
for (const healthIndicator of healthIndicators) {
try {
const result = await healthIndicator();
result && results.push(result);
} catch (error) {
// Is not an expected error. Throw further!
if (!error.causes) throw error;
// Is a expected health check error
errors.push((error as HealthCheckError).causes);
}
}

return { results, errors };
}

private getSummary(results: any[]): HealthIndicatorResult {
return results.reduce(
(previous: any, current: any) => Object.assign(previous, current),
{},
);
}

private getResult(results: any[], errors: any[]): HealthCheckResult {
const infoErrorCombined = (results || []).concat(errors || []);

const info = this.getSummary(results);
const error = this.getSummary(errors);
const details = this.getSummary(infoErrorCombined);

let status: HealthCheckStatus = 'ok';
status = errors.length > 0 ? 'error' : status;
status = this.isShuttingDown ? 'shutting_down' : status;

return {
status,
info,
error,
details,
};
}

/**
* Executes the given health indicators.
* Implementation for v6 compatibility.
Expand All @@ -89,7 +43,7 @@ export class HealthCheckExecutor implements BeforeApplicationShutdown {
const { results, errors } = await this.executeHealthIndicators(
healthIndicators,
);
const infoErrorCombined = (results || []).concat(errors || []);
const infoErrorCombined = results.concat(errors);

const details = this.getSummary(infoErrorCombined);

Expand Down Expand Up @@ -125,4 +79,50 @@ export class HealthCheckExecutor implements BeforeApplicationShutdown {
beforeApplicationShutdown() {
this.isShuttingDown = true;
}

private async executeHealthIndicators(
healthIndicators: HealthIndicatorFunction[],
) {
const results: any[] = [];
const errors: any[] = [];
for (const healthIndicator of healthIndicators) {
try {
const result = await healthIndicator();
result && results.push(result);
} catch (error) {
// Is not an expected error. Throw further!
if (!error.causes) throw error;
// Is a expected health check error
errors.push((error as HealthCheckError).causes);
}
}

return { results, errors };
}

private getSummary(results: any[]): HealthIndicatorResult {
return results.reduce(
(previous: any, current: any) => Object.assign(previous, current),
{},
);
}

private getResult(results: any[], errors: any[]): HealthCheckResult {
const infoErrorCombined = results.concat(errors);

const info = this.getSummary(results);
const error = this.getSummary(errors);
const details = this.getSummary(infoErrorCombined);

let status: HealthCheckStatus = 'ok';
status = errors.length > 0 ? 'error' : status;
status = this.isShuttingDown ? 'shutting_down' : status;

return {
status,
info,
error,
details,
};
}
}
7 changes: 4 additions & 3 deletions lib/health-check/health-check.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type * as Swagger from '@nestjs/swagger';
import { getHealthCheckSchema } from './health-check.schema';

type Swagger = typeof import('@nestjs/swagger');

const noop = () => {};

function getSwaggerDefinitions(swagger: typeof Swagger) {
function getSwaggerDefinitions(swagger: Swagger) {
const { ApiOkResponse, ApiServiceUnavailableResponse } = swagger;

// Possible HTTP Status
Expand All @@ -25,7 +26,7 @@ function getSwaggerDefinitions(swagger: typeof Swagger) {
}

export const HealthCheck = () => {
let swagger: typeof Swagger | null = null;
let swagger: Swagger | null = null;
try {
// Dynamically load swagger, in case it is not installed
swagger = require('@nestjs/swagger');
Expand Down
5 changes: 1 addition & 4 deletions lib/terminus-lib.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export const TerminusLibProvider = {
['@godaddy/terminus'],
'the legacy Terminus API',
);
if (!terminus) {
process.exit(1);
}
return terminus.createTerminus;
return terminus?.createTerminus;
},
};
Loading

0 comments on commit 14df600

Please sign in to comment.