Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRM: Handle Flurl http exceptions #309

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ namespace EvoSC.Modules.Official.WorldRecordModule.Interfaces;
public interface IWorldRecordService
{
/// <summary>
/// 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.
/// </summary>
/// <param name="mapUid">The UID of the map to load records from.</param>
/// <returns></returns>
public Task FetchRecordAsync(string mapUid);

/// <summary>
/// Clears the currently loaded world record.
/// </summary>
/// <returns></returns>
public Task ClearRecordAsync();

/// <summary>
/// Gets the currently loaded world record or null.
/// </summary>
Expand Down
23 changes: 11 additions & 12 deletions src/Modules/WorldRecordModule/Services/WorldRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ public async Task FetchRecordAsync(string mapUid)
User_Agent = "EvoSC# / World Record Grabber / Discord: chris92"
})
.GetJsonAsync<TMioLeaderboardResponse>();

}
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
Expand All @@ -56,7 +55,7 @@ public async Task FetchRecordAsync(string mapUid)
newWorldRecord.PlayerName,
newWorldRecord.Time.ToString()
);

await OverwriteRecordAsync(newWorldRecord);
}
else
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Loading