Better error handling for claim prize.
We wrap await function calls with try catch and return the res if any error occur.
This commit is contained in:
		@@ -65,15 +65,25 @@ const submitPrizeForWinnerById = async (req, res) => {
 | 
				
			|||||||
  const { id } = req.params;
 | 
					  const { id } = req.params;
 | 
				
			||||||
  const { wine } = req.body;
 | 
					  const { wine } = req.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const winner = await prizeDistribution.verifyWinnerNextInLine(id);
 | 
					  let prelotteryWine, winner;
 | 
				
			||||||
  const prelotteryWine = await lottery.wineById(wine.id);
 | 
					  try {
 | 
				
			||||||
 | 
					    prelotteryWine = await prelotteryWineRepository.wineById(wine._id);
 | 
				
			||||||
 | 
					    winner = await winnerRepository.winnerById(id, true);
 | 
				
			||||||
 | 
					  } catch (error) {
 | 
				
			||||||
 | 
					    const { statusCode, message } = error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return res.status(statusCode || 500).send({
 | 
				
			||||||
 | 
					      message: message || "Unexpected error occured while claiming prize.",
 | 
				
			||||||
 | 
					      success: false
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return prizeDistribution
 | 
					  return prizeDistribution
 | 
				
			||||||
    .claimPrize(winner, prelotteryWine)
 | 
					    .claimPrize(prelotteryWine, winner)
 | 
				
			||||||
    .then(_ => prizeDistribution.notifyNextWinner())
 | 
					    .then(_ => prizeDistribution.notifyNextWinner())
 | 
				
			||||||
    .then(_ =>
 | 
					    .then(_ =>
 | 
				
			||||||
      res.send({
 | 
					      res.send({
 | 
				
			||||||
        message: `${winner.name} successfully claimed prize: ${wine.name}`,
 | 
					        message: `${winner.name} successfully claimed prize: ${prelotteryWine.name}`,
 | 
				
			||||||
        success: true
 | 
					        success: true
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user