Skip to content

Commit

Permalink
Add error handling for HTTP request in AddEstimatedDateTime
Browse files Browse the repository at this point in the history
The method `AddEstimatedDateTimeOfPlayForEachPlay` in the `NhlGameService` class now includes error handling for the HTTP request to fetch the score report. A nullable string variable `scoreReport` is declared at the beginning of the method and initialized within a `try` block. A `catch` block has been added to handle exceptions, returning the `gameCenterPlayByPlay` object without further processing if an error occurs. This change improves the robustness of the method by preventing potential runtime errors due to failed HTTP requests.
  • Loading branch information
Afischbacher committed Nov 5, 2024
1 parent 7d78e54 commit b2337a7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Nhl.Api.Domain/Services/NhlGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ public class NhlGameService : INhlGameService
/// <returns>All of the game center play by play information with the time of play for each play</returns>
public async Task<GameCenterPlayByPlay> AddEstimatedDateTimeOfPlayForEachPlay(GameCenterPlayByPlay gameCenterPlayByPlay)
{
string? scoreReport = null;

var gameId = gameCenterPlayByPlay.Id.ToString(CultureInfo.InvariantCulture)[4..];
var scoreReport = await this._nhlScoresHtmlReportsApiHttpClient.GetStringAsync($"/{gameCenterPlayByPlay.Season}/PL{gameId}.HTM", default);

try
{
scoreReport = await this._nhlScoresHtmlReportsApiHttpClient.GetStringAsync($"/{gameCenterPlayByPlay.Season}/PL{gameId}.HTM", default);
}
catch
{
return gameCenterPlayByPlay;
}

var timePeriodMatches = Regex.Matches(scoreReport, @"(?<=<td class="" \+ bborder"">)Period(.*?)(?=</td>)", RegexOptions.Compiled | RegexOptions.CultureInvariant, TimeSpan.FromSeconds(5)).ToList();

Expand Down

0 comments on commit b2337a7

Please sign in to comment.