From f5d1bb759802ec5be041e21c2867191b7c57a4c2 Mon Sep 17 00:00:00 2001 From: "Chris (Flaeri)" Date: Mon, 30 Sep 2024 20:40:52 +0100 Subject: [PATCH] WRM: Handle Flurl http exceptions Instead of throwing, we log an error message, and let it continue. This will let it use the local author record, and not crash. Update comment to inform about the possiblity of it returning without completing in the event of API errors. Fixed whitespace and formatting --- .../Interfaces/IWorldRecordService.cs | 7 +++--- .../Services/WorldRecordService.cs | 23 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Modules/WorldRecordModule/Interfaces/IWorldRecordService.cs b/src/Modules/WorldRecordModule/Interfaces/IWorldRecordService.cs index dca17f43f..baf11cf64 100644 --- a/src/Modules/WorldRecordModule/Interfaces/IWorldRecordService.cs +++ b/src/Modules/WorldRecordModule/Interfaces/IWorldRecordService.cs @@ -6,18 +6,19 @@ namespace EvoSC.Modules.Official.WorldRecordModule.Interfaces; public interface IWorldRecordService { /// - /// Trigger a fetch for records from trackmania.io + /// Trigger a fetch for records from trackmania.io. + /// If an error occurs during the API fetch, the method will log the error and use AT instead. /// /// The UID of the map to load records from. /// public Task FetchRecordAsync(string mapUid); - + /// /// Clears the currently loaded world record. /// /// public Task ClearRecordAsync(); - + /// /// Gets the currently loaded world record or null. /// diff --git a/src/Modules/WorldRecordModule/Services/WorldRecordService.cs b/src/Modules/WorldRecordModule/Services/WorldRecordService.cs index b67273a5e..a0786f1b7 100644 --- a/src/Modules/WorldRecordModule/Services/WorldRecordService.cs +++ b/src/Modules/WorldRecordModule/Services/WorldRecordService.cs @@ -31,18 +31,17 @@ public async Task FetchRecordAsync(string mapUid) User_Agent = "EvoSC# / World Record Grabber / Discord: chris92" }) .GetJsonAsync(); - } catch (FlurlHttpException ex) { - logger.LogError(ex, "Invalid response from Openplanet. Maybe API issues?"); - throw; + // Log error regardless of the status code, since any exception means an unexpected outcome + logger.LogError(ex, "Error fetching data from trackmania.io API. Status code: {StatusCode}", ex.Call.Response?.StatusCode); } - + logger.LogDebug("Loaded records for map."); - if (res is {tops.Count: > 0}) + if (res is { tops.Count: > 0 }) { var bestRecord = res.tops.First(); var newWorldRecord = new WorldRecord @@ -56,7 +55,7 @@ public async Task FetchRecordAsync(string mapUid) newWorldRecord.PlayerName, newWorldRecord.Time.ToString() ); - + await OverwriteRecordAsync(newWorldRecord); } else @@ -69,12 +68,12 @@ public async Task FetchRecordAsync(string mapUid) Time = RaceTime.FromMilliseconds(mapInfo.AuthorTime), Source = WorldRecordSource.AuthorTime }; - + logger.LogDebug("Couldn't load World Record, using Author Time instead."); - + await OverwriteRecordAsync(newWorldRecord); } - + } public Task ClearRecordAsync() @@ -105,14 +104,14 @@ public async Task DetectNewWorldRecordThroughScoresAsync(ScoresEventArgs scoresE { await OverwriteRecordAsync(new WorldRecord { - PlayerName = score.Name, - Time = RaceTime.FromMilliseconds(score.BestRaceTime), + PlayerName = score.Name, + Time = RaceTime.FromMilliseconds(score.BestRaceTime), Source = WorldRecordSource.Local }); } } } - + private async Task OverwriteRecordAsync(WorldRecord newRecord) { lock (_currentWorldRecordLock)