reduce some potential errors

This commit is contained in:
RaidMax 2021-12-26 17:14:06 -06:00
parent 9baad44ab4
commit 67be4f8e7f
2 changed files with 26 additions and 5 deletions

View File

@ -19,7 +19,10 @@ using Data.Models.Client;
using Data.Models.Client.Stats; using Data.Models.Client.Stats;
using Data.Models.Server; using Data.Models.Server;
using Humanizer.Localisation; using Humanizer.Localisation;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Npgsql;
using Stats.Client.Abstractions; using Stats.Client.Abstractions;
using Stats.Config; using Stats.Config;
using Stats.Helpers; using Stats.Helpers;
@ -432,6 +435,12 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return null; return null;
} }
if (pl.ClientId <= 0)
{
_log.LogWarning("Stats for {Client} are not yet initialized", pl.ToString());
return null;
}
// get the client's stats from the database if it exists, otherwise create and attach a new one // get the client's stats from the database if it exists, otherwise create and attach a new one
// if this fails we want to throw an exception // if this fails we want to throw an exception
@ -511,6 +520,15 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return clientStats; return clientStats;
} }
catch (DbUpdateException updateException) when (
updateException.InnerException is PostgresException {SqlState: "23503"}
|| updateException.InnerException is SqliteException {SqliteErrorCode: 787}
|| updateException.InnerException is MySqlException {SqlState: "23503"})
{
_log.LogWarning("Trying to add {Client} to stats before they have been added to the database",
pl.ToString());
}
catch (Exception ex) catch (Exception ex)
{ {
_log.LogError(ex, "Could not add client to stats {@client}", pl.ToString()); _log.LogError(ex, "Could not add client to stats {@client}", pl.ToString());
@ -653,6 +671,12 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
var clientDetection = attacker.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY); var clientDetection = attacker.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY);
var clientStats = attacker.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY); var clientStats = attacker.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
if (clientDetection == null || clientStats?.ClientId == null)
{
_log.LogWarning("Client stats state for {Client} is not yet initialized", attacker.ToString());
return;
}
waiter = clientStats.ProcessingHit; waiter = clientStats.ProcessingHit;
await waiter.WaitAsync(Utilities.DefaultCommandTimeout, Plugin.ServerManager.CancellationToken); await waiter.WaitAsync(Utilities.DefaultCommandTimeout, Plugin.ServerManager.CancellationToken);

View File

@ -1,5 +1,4 @@
using IW4MAdmin.Plugins.Stats.Config; using IW4MAdmin.Plugins.Stats.Helpers;
using IW4MAdmin.Plugins.Stats.Helpers;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Dtos.Meta.Responses; using SharedLibraryCore.Dtos.Meta.Responses;
@ -15,9 +14,7 @@ using Data.Abstractions;
using Data.Models.Client; using Data.Models.Client;
using Data.Models.Client.Stats; using Data.Models.Client.Stats;
using Data.Models.Server; using Data.Models.Server;
using Humanizer;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore.Commands;
using IW4MAdmin.Plugins.Stats.Client.Abstractions; using IW4MAdmin.Plugins.Stats.Client.Abstractions;
using Stats.Client.Abstractions; using Stats.Client.Abstractions;
using Stats.Config; using Stats.Config;