Browse Source

Handle sendWebhook errors (#306)

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

4
server/src/controllers/analytics_controller.ts

@ -264,7 +264,11 @@ async function processDetectionResult(analyticUnitId: AnalyticUnit.AnalyticUnitI
const analyticUnit = await AnalyticUnit.findById(analyticUnitId); const analyticUnit = await AnalyticUnit.findById(analyticUnitId);
if(analyticUnit.alert) { if(analyticUnit.alert) {
if(!_.isEmpty(segments)) { if(!_.isEmpty(segments)) {
try {
sendWebhook(analyticUnit.name, _.last(segments)); sendWebhook(analyticUnit.name, _.last(segments));
} catch(err) {
console.error(`Error while sending webhook: ${err.message}`);
}
} }
} }
return { 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 // TODO: send webhook with payload without dep to AnalyticUnit
export async function sendWebhook(analyticUnitName: string, segment: Segment) { 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 = { const alert = {
analyticUnitName, analyticUnitName,
from: segment.from, from: segment.from,
@ -19,6 +15,10 @@ export async function sendWebhook(analyticUnitName: string, segment: Segment) {
console.log(`Sending alert: ${JSON.stringify(alert)}`); 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; let payload;
if(HASTIC_WEBHOOK_TYPE === 'application/json') { if(HASTIC_WEBHOOK_TYPE === 'application/json') {
payload = JSON.stringify(alert); payload = JSON.stringify(alert);

Loading…
Cancel
Save