Skip to content

Commit

Permalink
Merge pull request #105 from adityakalburgi/master
Browse files Browse the repository at this point in the history
Added Centralized Error Handling
  • Loading branch information
sakeel-103 authored Oct 10, 2024
2 parents a7eeccf + b81ebbf commit d6badfb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -122,4 +130,4 @@ process.on('SIGINT', async () => {
console.log('Shutting down gracefully...');
await mongoose.connection.close();
process.exit(0);
});
});

0 comments on commit d6badfb

Please sign in to comment.