Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Revert "chore: add Sentry configuration for better error handling" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier authored Jan 17, 2024
1 parent db9818e commit ab90516
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as indexController from '@controllers/indexController';
import * as resourceController from '@controllers/resourceController';
import * as hookController from '@controllers/hookController';
import * as pullRequestsController from '@controllers/pullRequestsController';
import * as sentry from './lib/sentry';
import EventBus from '@infra/eventBus/eventBus';
import { MARRAINAGE_EVENTS_VALUES } from '@models/marrainage';
import routes from './routes/routes';
Expand Down Expand Up @@ -40,7 +41,6 @@ import { corsOptions } from './utils/corsConfig';
import { errorHandler } from './middlewares/errorHandler';
import { setupSessionMiddleware } from './middlewares/sessionMiddleware';
import { PUBLIC_ROUTES } from './config/jwt.config';
import { initializeSentry, sentryErrorHandler } from './lib/sentry';

export const app = express();
app.set('trust proxy', 1);
Expand All @@ -53,7 +53,6 @@ app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './views/templates')); // the code is running in directory "dist".

// MIDDLEWARES
initializeSentry(app);
app.use(compression());
setupStaticFiles(app);
setupSessionMiddleware(app);
Expand Down Expand Up @@ -117,7 +116,8 @@ app.post(
express.json({ type: '*/*' }),
hookController.postToHook
);
app.use(sentryErrorHandler);

sentry.initCaptureConsoleWithHandler(app);

export default app.listen(config.port, () =>
console.log(`Running on: ${config.protocol}://${config.host}:${config.port}`)
Expand Down
41 changes: 26 additions & 15 deletions src/lib/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import * as Sentry from '@sentry/node';
import { ErrorRequestHandler } from 'express';
import config from '@/config';

export const initializeSentry = (app) => {
if (!config.sentryDNS) {
console.log('Sentry DSN not found. Sentry is not initialized.');
return;
}
import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations";
import config from '@config';

export function initCaptureConsole() {
const logLevel = ['error'];
console.log(
`Initializing Sentry for log level "${logLevel}" and config: ${config.sentryDNS}`
);
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
dsn: config.sentryDNS as string,
// https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole
integrations: [new CaptureConsoleIntegration({ levels: logLevel })],
});
}

app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
};
export function initCaptureConsoleWithHandler(app) {
if (config.sentryDNS) {
initCaptureConsole();

export const sentryErrorHandler: ErrorRequestHandler =
Sentry.Handlers.errorHandler();
// RequestHandler creates a separate execution context using domains, so that every
// transaction/span/breadcrumb is attached to its own Hub instance
app.use(Sentry.Handlers.requestHandler());

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
} else {
console.log(
'Sentry was not initialized as SENTRY_DNS env variable is missing'
);
}
}

0 comments on commit ab90516

Please sign in to comment.