Browse Source

Handle sendWebhook errors (#306)

pull/1/head
rozetko 6 years ago committed by Alexey Velikiy
parent
commit
d32b4ec479
  1. 6
      server/src/controllers/analytics_controller.ts
  2. 8
      server/src/services/notification_service.ts

6
server/src/controllers/analytics_controller.ts

@ -264,7 +264,11 @@ async function processDetectionResult(analyticUnitId: AnalyticUnit.AnalyticUnitI
const analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit.alert) {
if(!_.isEmpty(segments)) {
sendWebhook(analyticUnit.name, _.last(segments));
try {
sendWebhook(analyticUnit.name, _.last(segments));
} catch(err) {
console.error(`Error while sending webhook: ${err.message}`);
}
}
}
return {

8
server/src/services/notification_service.ts

@ -7,10 +7,6 @@ import * as querystring from 'querystring';
// TODO: send webhook with payload without dep to AnalyticUnit
export async function sendWebhook(analyticUnitName: string, segment: Segment) {
if(HASTIC_WEBHOOK_URL === null) {
throw new Error(`Can't send alert, HASTIC_WEBHOOK_URL is undefined`);
}
const alert = {
analyticUnitName,
from: segment.from,
@ -19,6 +15,10 @@ export async function sendWebhook(analyticUnitName: string, segment: Segment) {
console.log(`Sending alert: ${JSON.stringify(alert)}`);
if(HASTIC_WEBHOOK_URL === null) {
throw new Error(`Can't send alert, HASTIC_WEBHOOK_URL is undefined`);
}
let payload;
if(HASTIC_WEBHOOK_TYPE === 'application/json') {
payload = JSON.stringify(alert);

Loading…
Cancel
Save