Skip to content

Commit

Permalink
Refactor error handling in FreeGamesCommand
Browse files Browse the repository at this point in the history
Moved the exception handling for Reddit JSON loading into the FreeGamesCommand to centralize error management. This change ensures that any exceptions thrown during the retrieval of games from Reddit are caught and logged appropriately, depending on the VerboseLog setting. Simplified the GetPayload method in RedditHelper by removing redundant try-catch blocks.
  • Loading branch information
maxisoft committed May 22, 2024
1 parent e95b41b commit a3a5226
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
18 changes: 17 additions & 1 deletion ASFFreeGames/Commands/FreeGamesCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -202,7 +204,21 @@ private async Task<int> CollectGames(IEnumerable<Bot> bots, ECollectGameRequestS
int res = 0;

try {
ICollection<RedditGameEntry> games = await RedditHelper.GetGames(cancellationToken).ConfigureAwait(false);
ICollection<RedditGameEntry> games;

try {
games = await RedditHelper.GetGames(cancellationToken).ConfigureAwait(false);
}
catch (Exception e) when (e is InvalidOperationException or JsonException or IOException or RedditServerException) {
if (Options.VerboseLog ?? false) {
ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericException(e);
}
else {
ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericError($"Unable to load json from reddit {e.GetType().Name}: {e.Message}");
}

return 0;
}

LogNewGameCount(games, VerboseLog || requestSource is ECollectGameRequestSource.RequestedByUser);

Expand Down
11 changes: 1 addition & 10 deletions ASFFreeGames/Reddit/RedditHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ public static async ValueTask<ICollection<RedditGameEntry>> GetGames(Cancellatio
return result;
}

JsonNode jsonPayload;

try {
jsonPayload = await GetPayload(webBrowser, cancellationToken).ConfigureAwait(false);
}
catch (Exception e) when (e is InvalidOperationException or JsonException or IOException or RedditServerException) {
ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericError($"Unable to load json from reddit {e.GetType().Name}: {e.Message}");

return result;
}
JsonNode? jsonPayload = await GetPayload(webBrowser, cancellationToken).ConfigureAwait(false);

JsonNode? childrenElement = jsonPayload["data"]?["children"];

Expand Down

0 comments on commit a3a5226

Please sign in to comment.