Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Centralized Error Handling #105

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});