Skip to content

Commit

Permalink
Merge pull request #569 from studioph/435-print-line-on-failed-plugin…
Browse files Browse the repository at this point in the history
…s-parse

Print line number and offending entry on plugins parse failure
  • Loading branch information
Noggog authored Nov 27, 2024
2 parents f2036ae + 8c53da3 commit dba28bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,22 @@ public void CommentTrimming()
new LoadOrderListing("ModB.esp", false),
new LoadOrderListing("ModC.esp", true));
}

[Fact]
public void PrintLinesOnFailure()
{
var parser = new PluginListingsParser(
new PluginListingCommentTrimmer(),
new LoadOrderListingParser(
new HasEnabledMarkersInjection(true)));

parser.Invoking(p => p.Parse(GetStream(
@"*ModA.esm
ModB.esp
*Malformed"))
.ToList())
.Should()
.Throw<InvalidDataException>()
.WithMessage("Load order file had malformed entry at line 3: \"*Malformed\"");
}
}
12 changes: 11 additions & 1 deletion Mutagen.Bethesda.Core/Plugins/Order/DI/PluginListingsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ public PluginListingsParser(
/// <inheritdoc />
public IEnumerable<ILoadOrderListingGetter> Parse(Stream stream)
{
uint currentLine = 0;
using var streamReader = new StreamReader(stream);
while (!streamReader.EndOfStream)
{
currentLine++;
var str = streamReader.ReadLine().AsSpan();
str = _commentTrimmer.Trim(str);
if (MemoryExtensions.IsWhiteSpace(str) || str.Length == 0) continue;
yield return _listingParser.FromString(str);

if (_listingParser.TryFromString(str, out var listing))
{
yield return listing;
}
else
{
throw new InvalidDataException($"Load order file had malformed entry at line {currentLine}: \"{str}\"");
}
}
}
}

0 comments on commit dba28bf

Please sign in to comment.