Added exception handling.

This commit is contained in:
2024-02-15 22:32:57 +00:00
parent fc86c91b01
commit 31b48d795a
5 changed files with 231 additions and 147 deletions

View File

@@ -28,10 +28,11 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
return null; throw new Exception("Failed to Process Millionaire Maker Code.");
} }
public static void AssignMillionaireMakerToModelProperty(string propertyName, List<string> raffleNumbers, EurosResult eurosResult) public static void AssignMillionaireMakerToModelProperty(string propertyName, List<string> raffleNumbers, EurosResult eurosResult)

View File

@@ -23,7 +23,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
else else
{ {
return null; // ToDo: _logger.LogError
throw new Exception("machineName is null");
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -28,11 +28,9 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
//ToDo: This isn't ideal how will i know if it fails.
return false;
} }
public static void AssignDrawStatusToModelProperty(bool status, EurosResult eurosResult) public static void AssignDrawStatusToModelProperty(bool status, EurosResult eurosResult)
@@ -43,7 +41,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
} }
} }

View File

@@ -126,11 +126,9 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo Logger // ToDo: _logger.LogError
Console.WriteLine(); throw new Exception(ex.Message);
} }
return null;
} }
private static HtmlNode GetNodeByDataTitle(HtmlNode sectionRow, string dataTitle) private static HtmlNode GetNodeByDataTitle(HtmlNode sectionRow, string dataTitle)
@@ -143,10 +141,11 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
return null; throw new Exception("Failed to get node by data title.");
} }
public static void ProcessUKWinners(string sectionTitle, HtmlNode winnersNode, EurosResult eurosResult) public static void ProcessUKWinners(string sectionTitle, HtmlNode winnersNode, EurosResult eurosResult)
@@ -166,8 +165,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -179,8 +178,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -211,8 +210,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -255,8 +254,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -268,8 +267,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -290,8 +289,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -303,8 +302,8 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: _logger.LogError
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
} }
@@ -339,7 +338,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine(ex); throw new Exception(ex.Message);
} }
break; break;
@@ -372,7 +371,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine(ex); throw new Exception(ex.Message);
} }
break; break;
@@ -403,7 +402,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -434,7 +433,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -467,7 +466,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -498,7 +497,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -527,9 +526,9 @@ namespace lottery_co_uk_scraper.EuroMillions
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -562,7 +561,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -593,7 +592,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -624,7 +623,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;
@@ -724,7 +723,7 @@ namespace lottery_co_uk_scraper.EuroMillions
catch catch
{ {
// ToDo: Logger // ToDo: Logger
Console.WriteLine("Nothing to match"); throw new Exception(ex.Message);
} }
break; break;

View File

@@ -66,7 +66,8 @@ namespace lottery_co_uk_scraper.NationalLottery
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
} }
@@ -83,15 +84,17 @@ namespace lottery_co_uk_scraper.NationalLottery
} }
else else
{ {
return null; // ToDo: _logger.LogError
throw new Exception("sectionRow is null.");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
return null; throw new Exception("Failed to get section by row title.");
} }
private static HtmlNode GetNodeByDataTitle(HtmlNode sectionRow, string dataTitle) private static HtmlNode GetNodeByDataTitle(HtmlNode sectionRow, string dataTitle)
@@ -103,26 +106,37 @@ namespace lottery_co_uk_scraper.NationalLottery
public static void ProcessWinnersTable(HtmlDocument doc, LottoResult lottoResult) public static void ProcessWinnersTable(HtmlDocument doc, LottoResult lottoResult)
{ {
var table = doc.DocumentNode.Descendants("table") try
.FirstOrDefault(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("lotto mobFormat"));
if (table != null)
{ {
HtmlNode match2Section = GetSectionRowByTitle(table, LotteryTableRow.Match2); var table = doc.DocumentNode.Descendants("table")
if (match2Section != null) .FirstOrDefault(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("lotto mobFormat"));
if (table != null)
{ {
ProcessTableSection(table, LotteryTableRow.Match2, lottoResult); HtmlNode match2Section = GetSectionRowByTitle(table, LotteryTableRow.Match2);
if (match2Section != null)
{
ProcessTableSection(table, LotteryTableRow.Match2, lottoResult);
}
else
{
// ToDo: I don't need this write line, log the information about it being a draw with no match twos.
//_logger.LogInformation("");
Console.WriteLine("Section row with title 'Match 2' not found. Continuing without processing Match 2.");
}
ProcessTableSection(table, LotteryTableRow.Match3, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match4, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match5, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match5Bonus, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match6, lottoResult);
ProcessTableSection(table, LotteryTableRow.Total, lottoResult);
} }
else }
{ catch (Exception ex)
Console.WriteLine("Section row with title 'Match 2' not found. Continuing without processing Match 2."); {
} //_logger.LogError("");
ProcessTableSection(table, LotteryTableRow.Match3, lottoResult); throw new Exception(ex.Message);
ProcessTableSection(table, LotteryTableRow.Match4, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match5, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match5Bonus, lottoResult);
ProcessTableSection(table, LotteryTableRow.Match6, lottoResult);
ProcessTableSection(table, LotteryTableRow.Total, lottoResult);
} }
} }
@@ -130,126 +144,196 @@ namespace lottery_co_uk_scraper.NationalLottery
{ {
switch (sectionTitle) switch (sectionTitle)
{ {
#region Match 2
case LotteryTableRow.Match2: case LotteryTableRow.Match2:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched2), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched2), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched2Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched2PrizeFund), lottoResult, value);
}
}
catch(Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched2Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched2PrizeFund), lottoResult, value);
}
break;
#region Match 3
case LotteryTableRow.Match3: case LotteryTableRow.Match3:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched3), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched3), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched3Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched3PrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched3Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched3PrizeFund), lottoResult, value);
}
break;
#region Match 4
case LotteryTableRow.Match4: case LotteryTableRow.Match4:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched4), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched4), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched4Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched4PrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched4Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched4PrizeFund), lottoResult, value);
}
break;
#region Match 5
case LotteryTableRow.Match5: case LotteryTableRow.Match5:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched5), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched5), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5PrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched5Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5PrizeFund), lottoResult, value);
}
break;
#region Match 5 + Bonus
case LotteryTableRow.Match5Bonus: case LotteryTableRow.Match5Bonus:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched5PlusBonus), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched5PlusBonus), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5PlusBonusPrize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5PlusBonusPrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched5PlusBonusPrize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched5PlusBonusPrizeFund), lottoResult, value);
}
break;
#region Match 6
case LotteryTableRow.Match6: case LotteryTableRow.Match6:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched6), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalMatched6), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched6Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched6PrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizePerWinner) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.Matched6Prize), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.Matched6PrizeFund), lottoResult, value);
}
break;
#region Totals
case LotteryTableRow.Total: case LotteryTableRow.Total:
if (columnTitle == LotteryTableRow.Winners) try
{ {
PropertyManager.SetProperty(nameof(lottoResult.TotalWinners), lottoResult, value); if (columnTitle == LotteryTableRow.Winners)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalWinners), lottoResult, value);
}
if (columnTitle == LotteryTableRow.PrizeFundAmount)
{
PropertyManager.SetProperty(nameof(lottoResult.TotalPrizeFund), lottoResult, value);
}
}
catch (Exception ex)
{
// ToDo: _logger.LogError
throw new Exception(ex.Message);
} }
if (columnTitle == LotteryTableRow.PrizeFundAmount) break;
{ #endregion
PropertyManager.SetProperty(nameof(lottoResult.TotalPrizeFund), lottoResult, value);
}
break;
} }
} }