feat: Integrate Sentry to backend (#270)

* feat: Integrate Sentry to backend

* update lock

* update .env

---------

Co-authored-by: Rotimi Best <best@classroomio.com>
This commit is contained in:
Adeyemi Adetunji
2024-09-30 01:49:59 +01:00
committed by GitHub
co-authored by Rotimi Best
parent 9c066c79be
commit 2413120e95
6 changed files with 79 additions and 3 deletions
+28 -3
View File
@@ -4,6 +4,8 @@ const express = require('express');
const rateLimit = require('express-rate-limit');
const cors = require('cors');
const bodyParser = require('body-parser');
const sentry = require('@sentry/node');
const { ProfilingIntegration } = require('@sentry/profiling-node');
const uploadVideo = require('./src/routes/uploadVideo');
const downloadCertificate = require('./src/routes/downloadCertificate');
@@ -12,14 +14,19 @@ const downloadCourse = require('./src/routes/downloadCourse');
const katex = require('./src/routes/katex');
const sendEmail = require('./src/routes/sendEmail');
sentry.init({
dsn: process.env.SENTRY_DNS || '',
integrations: [new ProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0
});
// Express server
const app = express();
const port = process.env.PORT || 3002;
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
// Rate limiting middleware
@@ -30,6 +37,11 @@ const limiter = rateLimit({
app.use(limiter);
// Integrate Sentry middleware
app.use(sentry.Handlers.requestHandler());
app.use(sentry.Handlers.errorHandler());
// Register routes
app.use('/downloadCertificate', downloadCertificate);
app.use('/downloadLesson', downloadLesson);
app.use('/downloadCourse', downloadCourse);
@@ -41,10 +53,23 @@ app.get('/', (req, res) => {
res.send('Welcome to ClassroomIO');
});
// Start server
app.listen(port, () => {
console.log(`ClassroomIO Backend listening on port ${port}!`);
});
// Handle uncaught exceptions
process.on('uncaughtException', (err) => {
console.error('Uncaught error, err');
console.error('Uncaught error', err);
});
// Error-handling middleware
app.use((error, req, res, next) => {
console.error('Exception error caught:', error);
// Capture and send the error to Sentry
sentry.captureException(error);
// Respond with a 500 Internal Server Error
res.status(500).json({ error: 'Internal Server Error' });
});