From ac590fd41364a31a55f11b6dc54199a76e687636 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Sat, 27 Jul 2024 22:07:43 +0530 Subject: added some error handling features --- backend/index.js | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/backend/index.js b/backend/index.js index b23adc9..7f5ce96 100644 --- a/backend/index.js +++ b/backend/index.js @@ -80,62 +80,57 @@ const validate = (req, res, next) => { }; // Register route with validation and sanitization -app.post('/register', registerValidationRules(), validate, async (req, res) => { +app.post('/register', registerValidationRules(), validate, async (req, res, next) => { const { username, password } = req.body; - const hashedPassword = await bcrypt.hash(password, 10); - - // Save user to Firebase Firestore try { + const hashedPassword = await bcrypt.hash(password, 10); await db.collection('users').doc(username).set({ username, password: hashedPassword }); res.status(201).send('User registered'); } catch (error) { - console.error('Error registering user:', error); - res.status(500).send('Error registering user'); + next(error); // Pass the error to the error handling middleware } }); // Login route with validation and sanitization -app.post('/login', loginValidationRules(), validate, async (req, res) => { +app.post('/login', loginValidationRules(), validate, async (req, res, next) => { const { username, password } = req.body; try { const userDoc = await db.collection('users').doc(username).get(); if (!userDoc.exists) { - return res.status(400).send('Invalid username or password'); + return res.status(400).json({ error: 'Invalid username or password' }); } const user = userDoc.data(); const isPasswordValid = await bcrypt.compare(password, user.password); if (!isPasswordValid) { - return res.status(400).send('Invalid username or password'); + return res.status(400).json({ error: 'Invalid username or password' }); } const token = jwt.sign({ username: user.username }, JWT_SECRET, { expiresIn: '1h' }); res.json({ token }); } catch (error) { - console.error('Error during login:', error); - res.status(500).send('Error during login'); + next(error); // Pass the error to the error handling middleware } }); // Fetch ads route -app.get('/ads', async (req, res) => { +app.get('/ads', async (req, res, next) => { try { const adsSnapshot = await db.collection('ads').get(); const ads = adsSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); res.json(ads); } catch (error) { - console.error('Error fetching ads:', error); - res.status(500).send('Error fetching ads'); + next(error); // Pass the error to the error handling middleware } }); // Increment ad view count route -app.post('/ads/:id/view', async (req, res) => { +app.post('/ads/:id/view', async (req, res, next) => { const { id } = req.params; try { @@ -145,27 +140,36 @@ app.post('/ads/:id/view', async (req, res) => { }); res.status(200).json({ message: 'View count incremented' }); } catch (error) { - console.error('Error incrementing ad view count:', error); - res.status(500).send('Error incrementing ad view count'); + next(error); // Pass the error to the error handling middleware } }); // Get ad view counts route -app.get('/ads/:id/view-count', async (req, res) => { +app.get('/ads/:id/view-count', async (req, res, next) => { const { id } = req.params; try { const adDoc = await db.collection('ads').doc(id).get(); if (!adDoc.exists) { - return res.status(404).send('Ad not found'); + return res.status(404).json({ error: 'Ad not found' }); } res.json({ view_count: adDoc.data().view_count }); } catch (error) { - console.error('Error fetching ad view count:', error); - res.status(500).send('Error fetching ad view count'); + next(error); // Pass the error to the error handling middleware } }); +// Centralized error handling middleware +app.use((err, req, res, next) => { + console.error('Server Error:', err); + res.status(err.status || 500).json({ + error: { + message: err.message || 'Internal Server Error', + stack: process.env.NODE_ENV === 'development' ? err.stack : {} + } + }); +}); + // Start the server const PORT = process.env.PORT || 5000; app.listen(PORT, () => { -- cgit v1.2.3-59-g8ed1b