From 6dd515e8ebf9fef1b83ce54b8fcb131f7783a07e Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:02:47 +0200 Subject: [PATCH] Updating health controller to throw 503 if unhealthy --- src/health/controller.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/health/controller.ts b/src/health/controller.ts index 212b501..0395ef1 100644 --- a/src/health/controller.ts +++ b/src/health/controller.ts @@ -1,4 +1,9 @@ -import { Controller, Get, Inject } from '@nestjs/common' +import { + Controller, + Get, + Inject, + ServiceUnavailableException, +} from '@nestjs/common' import { EmailProvider } from '../email/provider' @@ -11,6 +16,11 @@ export class HealthController { @Get() getHealth() { - return this.emailProvider.isConnected() + const checks = [() => this.emailProvider.isConnected()] + + const healthy = checks.every((check) => check()) + if (!healthy) { + throw new ServiceUnavailableException() + } } }