Skip to content

Commit

Permalink
tracking unexpected errors (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Mar 11, 2024
1 parent 4167903 commit 7e0cd29
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}` })

})
Expand Down
21 changes: 18 additions & 3 deletions src/server/utils/analytics/analytics-event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
})
})

})
9 changes: 9 additions & 0 deletions src/server/utils/analytics/anyltics-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ export class AnalyticsEvent {
})
}
}

static reportUnexpectedError(error) {
if (this.isAnalyticEnabled()) {
analytics.track("unexpectedError", {
distinct_id: process.env.ANALYTICS_IDENTIFIER,
error,
})
}
}
}

0 comments on commit 7e0cd29

Please sign in to comment.