Skip to content

Commit

Permalink
WRM: Handle Flurl http exceptions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
flaeri committed Oct 5, 2024
1 parent 8f7dedb commit f5d1bb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
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

0 comments on commit f5d1bb7

Please sign in to comment.