diff --git a/src/app.ts b/src/app.ts index 345e329d..0534fb14 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,6 +15,7 @@ import { NextFunction, Request, Response } from "express" import { PgError } from "./server/errors/pgError" import { bree } from "./server/utils/scheduled-tasks/scheduler" import helmet from "helmet" +import { AnalyticsEvent } from "./server/utils/analytics/anyltics-event" const DEFAULT_PORT = 5000 const PORT = process.env.PORT || DEFAULT_PORT @@ -63,6 +64,7 @@ export class App { } const errorId = uuidv4() logger.error(`Unexpected error: ${error}, errorId: ${errorId}`) + AnalyticsEvent.reportUnexpectedError(error) return res.status(StatusCode.InternalError).json({ message: `Unexpected error occurred: ${errorId}` }) }) diff --git a/src/server/utils/analytics/analytics-event.spec.ts b/src/server/utils/analytics/analytics-event.spec.ts index 24d2c9c7..35258679 100644 --- a/src/server/utils/analytics/analytics-event.spec.ts +++ b/src/server/utils/analytics/analytics-event.spec.ts @@ -36,7 +36,7 @@ describe("AnalyticEvents", () => { expect(trackMock).not.toHaveBeenCalled() }) - it("should track the even only when analytics enabled", function () { + it("should track the event only when analytics enabled", function () { process.env.OPT_OUT_ANALYTICS = "false" const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined) AnalyticsEvent.reportProcessingFinished() @@ -52,7 +52,7 @@ describe("AnalyticEvents", () => { expect(trackMock).not.toHaveBeenCalled() }) - it("should track the even only when analytics enabled", function () { + it("should track the event only when analytics enabled", function () { process.env.OPT_OUT_ANALYTICS = "false" const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined) AnalyticsEvent.reportDetails(1, 1) @@ -68,12 +68,27 @@ describe("AnalyticEvents", () => { expect(trackMock).not.toHaveBeenCalled() }) - it("should track the even only when analytics enabled", function () { + it("should track the event only when analytics enabled", function () { process.env.OPT_OUT_ANALYTICS = "false" const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined) AnalyticsEvent.reportProcessingStarted() expect(trackMock).toHaveBeenCalled() }) }) + describe("unexpectedError", () => { + it("should not track the event when analytics disabled", function () { + process.env.OPT_OUT_ANALYTICS = "true" + const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined) + AnalyticsEvent.reportUnexpectedError(Error("test")) + expect(trackMock).not.toHaveBeenCalled() + + }) + it("should track the event only when analytics enabled", function () { + process.env.OPT_OUT_ANALYTICS = "false" + const trackMock = (analytics.track as any).mockResolvedValueOnce(undefined) + AnalyticsEvent.reportUnexpectedError(Error("test")) + expect(trackMock).toHaveBeenCalled() + }) + }) }) diff --git a/src/server/utils/analytics/anyltics-event.ts b/src/server/utils/analytics/anyltics-event.ts index d1580f7e..91dbf666 100644 --- a/src/server/utils/analytics/anyltics-event.ts +++ b/src/server/utils/analytics/anyltics-event.ts @@ -34,4 +34,13 @@ export class AnalyticsEvent { }) } } + + static reportUnexpectedError(error) { + if (this.isAnalyticEnabled()) { + analytics.track("unexpectedError", { + distinct_id: process.env.ANALYTICS_IDENTIFIER, + error, + }) + } + } }