From 8b618d8801adc9673970758816286a47c2357ed7 Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Tue, 30 Jul 2024 18:19:01 +0200 Subject: [PATCH] fix(api): handle failing to send push notification (#930) --- .../gateways/notification.web-push.gateway.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/services/api/src/core/notifications/client/gateways/notification.web-push.gateway.ts b/services/api/src/core/notifications/client/gateways/notification.web-push.gateway.ts index 2605037a..1ca32697 100644 --- a/services/api/src/core/notifications/client/gateways/notification.web-push.gateway.ts +++ b/services/api/src/core/notifications/client/gateways/notification.web-push.gateway.ts @@ -1,6 +1,7 @@ import { Injectable } from "@nestjs/common"; import { sendNotification, setVapidDetails } from "web-push"; +import { Logger } from "src/core/logging"; import { getRequiredStringConfig } from "src/utils/config.helper"; import { NotificationSubscriptionService } from "../../domain/services/notification-subscription.service"; @@ -8,6 +9,8 @@ import { INotification } from "../../types"; @Injectable() export class NotificationWebPushGateway { + private logger = new Logger(NotificationWebPushGateway.name); + constructor( private readonly notificationSubscriptionService: NotificationSubscriptionService, ) { @@ -29,12 +32,20 @@ export class NotificationWebPushGateway { return false; } - const result = await sendNotification( - pushSubscription, - JSON.stringify(notification), - ); - - return result.statusCode < 400; + try { + const result = await sendNotification( + pushSubscription, + JSON.stringify(notification), + ); + + return result.statusCode < 400; + } catch (error) { + this.logger.error("Unable to send push notification", { + error, + notification, + }); + return false; + } } public async sendWebPush(