From b81ebbf4d4796bb12b96f2cc8fed69b498da60b0 Mon Sep 17 00:00:00 2001 From: Aditya kalburgi <124163311+adityakalburgi@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:04:20 +0530 Subject: [PATCH] Update server.js --- backend/server.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/server.js b/backend/server.js index eae94eb..c042fed 100644 --- a/backend/server.js +++ b/backend/server.js @@ -112,6 +112,14 @@ app.post('/api/logout', (req, res) => { res.json({ message: 'Logout successful' }); // Send a JSON response }); }); +// Centralized Error Handling Middleware +app.use((err, req, res, next) => { + console.error(err.stack); // Log the error stack trace for debugging purposes + res.status(500).json({ + message: 'An internal server error occurred', + error: process.env.NODE_ENV === 'development' ? err.message : {} // Only send the error message in development mode + }); +}); // Start the server const PORT = process.env.PORT || 5000; @@ -122,4 +130,4 @@ process.on('SIGINT', async () => { console.log('Shutting down gracefully...'); await mongoose.connection.close(); process.exit(0); -}); \ No newline at end of file +});