Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Fluid.Tests/ErrorMessagesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ public void WrongTemplateShouldRenderErrorMessage(string source, string expected
Assert.False(_parser.TryParse(source, out var _, out var error));
Assert.StartsWith(expected, error); // e.g., message then 'at (1:15)'
}

[Theory]
[InlineData(" {% assign a 'b' %}")]
[InlineData("{% assign a = 'b' %}\n {% assign a 'b' %}")]
[InlineData(" {% assign a 'b' %}\n {% assign a = 'b' %}")]
[InlineData(" {% assign a 'b' %}\n\r {% assign a = 'b' %}")]
[InlineData(" {% assign a 'b' %}\n{% assign a = 'b' %}\n {% assign a 'b' %}")]
public void ErrorMessageContainsLineSource(string source)
{
Assert.False(_parser.TryParse(source, out var _, out var error));
Assert.Contains("Source:\n {% assign a 'b' %}", error);
}
}
}
9 changes: 8 additions & 1 deletion Fluid/FluidParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public static IFluidTemplate Parse(this FluidParser parser, string template)

if (parlotError != null)
{
throw new ParseException($"{parlotError.Message} at {parlotError.Position}");
// Extract line with error
var start = parlotError.Position.Offset - 1;
var end = parlotError.Position.Offset;
while (start > 0 && template[start] != '\n' && template[start] != '\r') start--;
while (end < template.Length && template[end] != '\n') end++;
var source = template.Substring(start, end - start).Trim('\n', '\r');

throw new ParseException($"{parlotError.Message} at {parlotError.Position}\nSource:\n{source}");
}

if (!success)
Expand Down