From 15bb52015e66ebfef03b23fc37fb63284e9f416f Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Tue, 2 Jan 2024 17:52:22 +0100 Subject: [PATCH] :sparkles: Installing sentry --- package.json | 1 + src/app/middlewares/ErrorHandler.ts | 13 +++++++++++++ src/entries/App.ts | 3 ++- src/sentry.config.ts | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/sentry.config.ts diff --git a/package.json b/package.json index 65a5d8c7..a7ad6158 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@mailchimp/mailchimp_transactional": "^1.0.50", "@pinata/sdk": "^2.1.0", "@prisma/client": "^4.11.0", + "@sentry/node": "^7.91.0", "adm-zip": "^0.5.10", "axios": "^1.6.2", "bcrypt": "^5.1.1", diff --git a/src/app/middlewares/ErrorHandler.ts b/src/app/middlewares/ErrorHandler.ts index 274f5923..662e7f7f 100644 --- a/src/app/middlewares/ErrorHandler.ts +++ b/src/app/middlewares/ErrorHandler.ts @@ -1,6 +1,7 @@ import HttpException from "@Common/system/controller-pattern/exceptions/HttpException"; import HttpCodes from "@Common/system/controller-pattern/HttpCodes"; import { NextFunction, Request, Response } from "express"; +import * as Sentry from "@sentry/node"; export default function errorHandler(error: any, req: Request, response: Response, next: NextFunction) { const errorStatus: number = error["status"]; @@ -15,6 +16,18 @@ export default function errorHandler(error: any, req: Request, response: Respons return; } + const transaction = Sentry.startTransaction({ + op: "Crashed Application", + name: "Crashed Application", + }); + + if (error instanceof Error) { + Sentry.captureException(error.stack); + } else { + Sentry.captureException(error); + } + transaction.finish(); + if (error instanceof HttpException) { response.status(error.httpCode).send(error.message); return; diff --git a/src/entries/App.ts b/src/entries/App.ts index fcee2984..5dd1bd04 100644 --- a/src/entries/App.ts +++ b/src/entries/App.ts @@ -8,12 +8,13 @@ import bodyParser from "body-parser"; import errorHandler from "@App/middlewares/ErrorHandler"; import { BackendVariables } from "@Common/config/variables/Variables"; import multer from "multer"; +import "../sentry.config"; const storage = multer.memoryStorage(); (async () => { try { - const variables = await Container.get(BackendVariables).validate(); + const variables = await Container.get(BackendVariables).validate(); const port = variables.APP_PORT; const rootUrl = variables.APP_ROOT_URL; const label = variables.APP_LABEL ?? "Unknown Service"; diff --git a/src/sentry.config.ts b/src/sentry.config.ts new file mode 100644 index 00000000..a921142d --- /dev/null +++ b/src/sentry.config.ts @@ -0,0 +1,16 @@ +import { BackendVariables } from "@Common/config/variables/Variables"; +import * as Sentry from "@sentry/node"; +import Container from "typedi"; + +const variables = Container.get(BackendVariables); + +Sentry.init({ + dsn: "https://ca6a89e8b480c814e1b5828b8412c681@o4506382103281664.ingest.sentry.io/4506399972130816", + + // We recommend adjusting this value in production, or using tracesSampler + // for finer control + tracesSampleRate: 1.0, + environment: variables.ENV, +}); + +Sentry.setTag("service", "leCoffre-back");