Skip to content

Commit

Permalink
Log inner exceptions instead of AggregateException (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Dec 8, 2024
1 parent e1e11cc commit 057cf33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
47 changes: 19 additions & 28 deletions ImperatorToCK3/CK3/Characters/CharacterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,25 @@ Configuration config
MaxDegreeOfParallelism = Environment.ProcessorCount - 1,
};

try {
Parallel.ForEach(impWorld.Characters, parallelOptions, irCharacter => {
ImportImperatorCharacter(
irCharacter,
religionMapper,
cultureMapper,
traitMapper,
nicknameMapper,
impWorld.LocDB,
ck3LocDB,
impWorld.MapData,
provinceMapper,
deathReasonMapper,
dnaFactory,
conversionDate,
config,
unlocalizedImperatorNames
);
});
} catch (AggregateException e) {
var innerException = e.InnerExceptions[0];
Logger.Error("Exception thrown during Imperator characters import: " + innerException.Message);
Logger.Debug("Exception stack trace: " + innerException.StackTrace);

// Rethrow the inner exception to stop the program.
throw innerException;
}

Parallel.ForEach(impWorld.Characters, parallelOptions, irCharacter => {
ImportImperatorCharacter(
irCharacter,
religionMapper,
cultureMapper,
traitMapper,
nicknameMapper,
impWorld.LocDB,
ck3LocDB,
impWorld.MapData,
provinceMapper,
deathReasonMapper,
dnaFactory,
conversionDate,
config,
unlocalizedImperatorNames
);
});

if (unlocalizedImperatorNames.Any()) {
Logger.Warn("Found unlocalized Imperator names: " + string.Join(", ", unlocalizedImperatorNames));
}
Expand Down
16 changes: 11 additions & 5 deletions ImperatorToCK3/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using log4net.Core;
using System;
using System.Globalization;
using System.Linq;

namespace ImperatorToCK3;
public static class Program {
Expand All @@ -22,14 +23,19 @@ public static int Main(string[] args) {
}
Converter.ConvertImperatorToCK3(converterVersion);
return 0;
} catch (Exception e) {
Logger.Log(Level.Fatal, e is UserErrorException ? e.Message : $"{e.GetType()}: {e.Message}");
if (e.StackTrace is not null) {
Logger.Debug(e.StackTrace);
} catch (Exception ex) {
// If the exception is an AggregateException, we want the original inner exception's stack trace.
if (ex is AggregateException aggregateEx) {
ex = aggregateEx.Flatten().InnerExceptions.FirstOrDefault() ?? ex;
}

Logger.Log(Level.Fatal, ex is UserErrorException ? ex.Message : $"{ex.GetType()}: {ex.Message}");
if (ex.StackTrace is not null) {
Logger.Debug(ex.StackTrace);
}

// Return exit code 1 for user errors. They should not be reported to Sentry.
if (e is UserErrorException) {
if (ex is UserErrorException) {
return 1;
}
return -1;
Expand Down

0 comments on commit 057cf33

Please sign in to comment.