From 21d575651a89c0cf6cd209895e17c649c6973c6b Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Fri, 19 Jul 2019 00:23:34 +0300 Subject: [PATCH] Exception if hastic webhook url is undefined #593 (#718) --- server/src/services/alert_service.ts | 14 +++++++++++--- server/src/services/notification_service.ts | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/src/services/alert_service.ts b/server/src/services/alert_service.ts index 7980f7b..99bb1ac 100644 --- a/server/src/services/alert_service.ts +++ b/server/src/services/alert_service.ts @@ -19,7 +19,11 @@ export class Alert { protected async send(segment) { const notification = await this.makeNotification(segment); - sendNotification(notification); + try { + await sendNotification(notification); + } catch(error) { + console.error(`can't send notification ${error}`); + }; } protected async makeNotification(segment: Segment): Promise { @@ -187,7 +191,7 @@ export class AlertService { this._alerts[id].receive(segment); }; - public sendMsg(message: string, type: WebhookType, optionalInfo = {}) { + public async sendMsg(message: string, type: WebhookType, optionalInfo = {}) { const now = Date.now(); const infoAlert: InfoMeta = { params: optionalInfo, @@ -195,7 +199,11 @@ export class AlertService { from: now, to: now } - sendNotification({ message, meta: infoAlert }); + try { + await sendNotification({ message, meta: infoAlert }) + } catch(error) { + console.error(`can't send notification ${error}`); + }; } public sendGrafanaAvailableWebhook() { diff --git a/server/src/services/notification_service.ts b/server/src/services/notification_service.ts index b27191e..2490496 100644 --- a/server/src/services/notification_service.ts +++ b/server/src/services/notification_service.ts @@ -43,7 +43,8 @@ export declare type Notification = { export async function sendNotification(notification: Notification) { if(HASTIC_WEBHOOK_URL === null) { - throw new Error(`Can't send notification, HASTIC_WEBHOOK_URL is undefined`); + console.log(`HASTIC_WEBHOOK_URL is not set, skip sending notification: ${notification.message}`); + return; } notification.message += `\nInstance: ${HASTIC_INSTANCE_NAME}`;