Log errors before throwing exceptions

This commit is contained in:
Cj
2025-08-12 15:49:26 -04:00
parent 628a31f07f
commit 021e5f8239
6 changed files with 101 additions and 43 deletions
@@ -21,7 +21,7 @@ public class BotDifficultyHelper(
ICloner cloner
)
{
protected readonly PmcConfig _pmcConfig = configServer.GetConfig<PmcConfig>();
protected readonly PmcConfig PMCConfig = configServer.GetConfig<PmcConfig>();
/// <summary>
/// Get difficulty settings for desired bot type, if not found use assault bot types
@@ -72,9 +72,9 @@ public class BotDifficultyHelper(
/// <returns>Difficulty object</returns>
protected DifficultyCategories? GetDifficultySettings(string type, string difficulty)
{
var difficultySetting = string.Equals(_pmcConfig.Difficulty, "asonline", StringComparison.OrdinalIgnoreCase)
var difficultySetting = string.Equals(PMCConfig.Difficulty, "asonline", StringComparison.OrdinalIgnoreCase)
? difficulty
: _pmcConfig.Difficulty.ToLowerInvariant();
: PMCConfig.Difficulty.ToLowerInvariant();
difficultySetting = ConvertBotDifficultyDropdownToBotDifficulty(difficultySetting);
@@ -41,7 +41,9 @@ public class DurabilityLimitsHelper(
var itemMaxDurability = itemTemplate?.Properties?.MaxDurability;
if (!itemMaxDurability.HasValue)
{
throw new DurabilityHelperException("Item max durability amount is null when trying to get max armor durability");
const string message = "Item max durability amount is null when trying to get max armor durability";
logger.Error(message);
throw new DurabilityHelperException(message);
}
if (botRole is null)
@@ -158,7 +160,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var durability))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return durability.Weapon.LowestMax;
@@ -177,7 +181,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var durability))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return durability.Weapon.HighestMax;
@@ -221,7 +227,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var durability))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return durability.Weapon.MinDelta;
@@ -241,7 +249,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var value))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return value.Weapon.MaxDelta;
@@ -261,7 +271,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var value))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return value.Armor.MinDelta;
@@ -281,7 +293,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var value))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return value.Armor.MaxDelta;
@@ -301,7 +315,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var value))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return value.Armor.MinLimitPercent;
@@ -321,7 +337,9 @@ public class DurabilityLimitsHelper(
if (!_botConfig.Durability.BotDurabilities.TryGetValue(botRole, out var value))
{
throw new DurabilityHelperException($"Bot role {botRole} durability doesn't exist");
var message = $"Bot role {botRole} durability doesn't exist";
logger.Error(message);
throw new DurabilityHelperException(message);
}
return value.Weapon.MinLimitPercent;
@@ -74,7 +74,9 @@ public class HandbookHelper(ISptLogger<HandbookHelper> logger, DatabaseService d
{
if (!result.Categories.ById.TryAdd(handbookCategory.Id, handbookCategory.ParentId))
{
throw new HandbookHelperException($"Unable to add `{handbookCategory.Id}`. Key already exists.");
var message = $"Unable to add `{handbookCategory.Id}`. Key already exists.";
logger.Error(message);
throw new HandbookHelperException(message);
}
if (handbookCategory.ParentId is not null)
@@ -4,6 +4,7 @@ using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers;
using SPTarkov.Server.Core.Utils;
using BodyPartHealth = SPTarkov.Server.Core.Models.Eft.Common.Tables.BodyPartHealth;
@@ -11,7 +12,7 @@ using BodyPartHealth = SPTarkov.Server.Core.Models.Eft.Common.Tables.BodyPartHea
namespace SPTarkov.Server.Core.Helpers;
[Injectable]
public class HealthHelper(TimeUtil timeUtil, ConfigServer configServer)
public class HealthHelper(ISptLogger<HealthHelper> logger, TimeUtil timeUtil, ConfigServer configServer)
{
protected readonly HealthConfig HealthConfig = configServer.GetConfig<HealthConfig>();
protected readonly HashSet<string> EffectsToSkip = ["Dehydration", "Exhaustion"];
@@ -35,7 +36,9 @@ public class HealthHelper(TimeUtil timeUtil, ConfigServer configServer)
if (healthChanges.BodyParts is null)
{
throw new HealthHelperException("healthChanges.BodyParts is null when trying to apply health changes");
const string message = "healthChanges.BodyParts is null when trying to apply health changes";
logger.Error(message);
throw new HealthHelperException(message);
}
// Alter saved profiles Health with values from post-raid client data
@@ -46,7 +49,9 @@ public class HealthHelper(TimeUtil timeUtil, ConfigServer configServer)
if (pmcProfileToUpdate.Health is null)
{
throw new HealthHelperException("pmcProfileToUpdate.Health is null when trying to apply health changes");
const string message = "pmcProfileToUpdate.Health is null when trying to apply health changes";
logger.Error(message);
throw new HealthHelperException(message);
}
// Update last edited timestamp
@@ -76,9 +81,10 @@ public class HealthHelper(TimeUtil timeUtil, ConfigServer configServer)
if (partProperties.Health is null || matchingProfilePart.Health is null)
{
throw new HealthHelperException(
"partProperties.Health or matchingBodyPart.Health is null when trying to modify profile health properties"
);
const string message =
"partProperties.Health or matchingBodyPart.Health is null when trying to modify profile health properties";
logger.Error(message);
throw new HealthHelperException(message);
}
if (HealthConfig.Save.Health)
@@ -57,7 +57,9 @@ public class HideoutHelper(
if (pmcData.Hideout is null)
{
throw new HideoutHelperException($"Hideout is null when trying to register production for recipe id `{recipe.Id}`");
var message = $"Hideout is null when trying to register production for recipe id `{recipe.Id}`";
logger.Error(message);
throw new HideoutHelperException(message);
}
// @Important: Here we need to be very exact:
@@ -131,9 +133,9 @@ public class HideoutHelper(
if (pmcData.Hideout is null)
{
throw new HideoutHelperException(
$"Hideout is null when trying to register production for recipe id `{productionRequest.RecipeId.Value}`"
);
var message = $"Hideout is null when trying to register production for recipe id `{productionRequest.RecipeId.Value}`";
logger.Error(message);
throw new HideoutHelperException(message);
}
// @Important: Here we need to be very exact:
@@ -209,7 +211,9 @@ public class HideoutHelper(
case BonusType.MaximumEnergyReserve:
if (profileData.Health?.Energy is null)
{
throw new HideoutHelperException("Profile Energy is null when trying to apply MaximumEnergyReserve");
const string message = "Profile Energy is null when trying to apply MaximumEnergyReserve";
logger.Error(message);
throw new HideoutHelperException(message);
}
// Amend max energy in profile
@@ -231,7 +235,9 @@ public class HideoutHelper(
if (profileData.Bonuses is null)
{
throw new HideoutHelperException($"Profile bonuses are null when trying to add: {bonus.Type}");
var message = $"Profile bonuses are null when trying to add: {bonus.Type}";
logger.Error(message);
throw new HideoutHelperException(message);
}
// Add bonus to player bonuses array in profile
@@ -255,7 +261,9 @@ public class HideoutHelper(
if (pmcData.Hideout is null)
{
throw new HideoutHelperException("Hideout is null when trying to update player hideout");
const string message = "Hideout is null when trying to update player hideout";
logger.Error(message);
throw new HideoutHelperException(message);
}
pmcData.Hideout.SptUpdateLastRunTimestamp ??= timeUtil.GetTimeStamp();
@@ -402,7 +410,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to update water collector production timer");
const string message = "Hideout productions are null when trying to update water collector production timer";
logger.Error(message);
throw new HideoutHelperException(message);
}
if (!pmcData.Hideout.Production.TryGetValue(productionId, out var production) || production is null)
@@ -430,7 +440,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to update production progress");
const string message = "Hideout productions are null when trying to update production";
logger.Error(message);
throw new HideoutHelperException(message);
}
// Production is complete, no need to do any calculations
@@ -472,7 +484,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to update cultist progress");
const string message = "Hideout productions are null when trying to update cultist progress";
logger.Error(message);
throw new HideoutHelperException(message);
}
if (!pmcData.Hideout.Production.TryGetValue(prodId, out var production) || production is null)
@@ -528,7 +542,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to match production progress");
const string message = "Hideout productions are null when trying to update production";
logger.Error(message);
throw new HideoutHelperException(message);
}
// Production doesn't exist or progress or production time is null
@@ -558,7 +574,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to update scav case production timer");
const string message = "Hideout productions are null when trying to update scav case";
logger.Error(message);
throw new HideoutHelperException(message);
}
if (!pmcData.Hideout.Production.TryGetValue(productionId, out var production) || production is null)
@@ -583,7 +601,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout is null)
{
throw new HideoutHelperException("Hideout is null when trying update areas with resources");
const string message = "Hideout is null when trying update areas with resources";
logger.Error(message);
throw new HideoutHelperException(message);
}
foreach (var area in pmcData.Hideout.Areas ?? [])
@@ -1217,7 +1237,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout is null)
{
throw new HideoutHelperException("Pmc Hideout is null when trying get last elasped server tick");
const string message = "Pmc Hideout is null when trying get last elapsed server tick";
logger.Error(message);
throw new HideoutHelperException(message);
}
// Reduce time elapsed (and progress) when generator is off
@@ -1341,7 +1363,9 @@ public class HideoutHelper(
{
if (pmcData.Hideout?.Production is null)
{
throw new HideoutHelperException("Hideout productions are null when trying to retrieve bitcoin productions");
const string message = "Hideout productions are null when trying to retrieve bitcoin productions";
logger.Error(message);
throw new HideoutHelperException(message);
}
if (pmcData.Hideout?.Production?.TryGetValue(BitcoinProductionId, out var bitcoinCraft) is null or false)
@@ -42,7 +42,9 @@ public class InRaidHelper(
{
if (serverProfile.InsuredItems is null)
{
throw new InRaidHelperException("Insured items are null when trying to set inventory post raid");
const string message = "Insured items are null when trying to set inventory post raid";
logger.Error(message);
throw new InRaidHelperException(message);
}
if (
@@ -51,9 +53,10 @@ public class InRaidHelper(
|| serverProfile.Inventory?.Equipment is null
)
{
throw new InRaidHelperException(
"Server profile inventory items, quest raid items, or equipment are null when trying to set inventory post raid"
);
const string message =
"Server profile inventory items, quest raid items, or equipment are null when trying to set inventory post raid";
logger.Error(message);
throw new InRaidHelperException(message);
}
if (
@@ -62,9 +65,10 @@ public class InRaidHelper(
|| postRaidProfile.Inventory.Equipment is null
)
{
throw new InRaidHelperException(
"Post raid profile inventory items, quest raid items, or equipment are null when trying to set inventory post raid"
);
const string message =
"Post raid profile inventory items, quest raid items, or equipment are null when trying to set inventory post raid";
logger.Error(message);
throw new InRaidHelperException(message);
}
// Store insurance (as removeItem() removes insured items)
@@ -160,7 +164,9 @@ public class InRaidHelper(
{
if (pmcData.Inventory is null)
{
throw new InRaidHelperException("Pmc profile inventory is null when trying to delete inventory");
const string message = "Pmc profile inventory is null when trying to delete inventory";
logger.Error(message);
throw new InRaidHelperException(message);
}
// Get inventory items to remove from players profile
@@ -222,7 +228,9 @@ public class InRaidHelper(
{
if (pmcData.Inventory is null)
{
throw new InRaidHelperException("Pmc profile inventory is null when checking if an item is kept on death");
const string message = "Pmc profile inventory is null when checking if an item is kept on death";
logger.Error(message);
throw new InRaidHelperException(message);
}
// Base inventory items are always kept