Skip to content

Commit 1194bdc

Browse files
author
Mike Bridge
committed
make LiquidParsingException non-public
1 parent 7bfb328 commit 1194bdc

File tree

5 files changed

+71
-54
lines changed

5 files changed

+71
-54
lines changed

Liquid.NET.Tests/Ruby/LiquidTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,19 +549,19 @@ public void It_Should_Generate_An_Exception(String input, String assigns, String
549549
}
550550
var template = LiquidTemplate.Create(input);
551551
IList<LiquidError> errors = new List<LiquidError>();
552-
try
553-
{
552+
// try
553+
// {
554554
String result = template.LiquidTemplate.Render(ctx, onRenderingError: errors.Add);
555555
//Console.WriteLine("RenderingErrors")
556556
Assert.That(errors.Count, Is.EqualTo(1));
557557
Assert.That(errors[0].ToString(), Is.StringContaining(expectedMessage));
558558
//Assert.Fail("Expected exception: " + expectedMessage);
559-
}
560-
catch (LiquidParserException ex)
561-
{
559+
// }
560+
// catch (LiquidParserException ex)
561+
// {
562562
// Assert
563-
Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
564-
}
563+
//Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
564+
// }
565565
//catch (LiquidRendererException ex)
566566
//{
567567
// Assert

Liquid.NET.Tests/Tags/CustomBlockTagTests.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,25 @@ public void It_Should_Not_Parse_A_Custom_BlockTag_With_No_End()
4141
{
4242
// Act
4343
var templateContext = new TemplateContext().WithAllFilters().WithCustomTagBlockRenderer<WordReverserBlockTag>("echoargs");
44-
try
45-
{
46-
RenderingHelper.RenderTemplate(
47-
"Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}", templateContext);
48-
Assert.Fail("This should have thrown an error.");
49-
}
50-
catch (LiquidParserException ex)
51-
{
52-
var allErrors = String.Join(",", ex.LiquidErrors.Select(x => x.ToString()));
53-
Logger.Log(allErrors);
54-
Assert.That(allErrors, Is.StringContaining("There was no opening tag for the ending tag 'endsomethingelse'"));
55-
}
44+
45+
var templateResult = LiquidTemplate.Create("Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}");
46+
Assert.That(templateResult.HasParsingErrors);
47+
Assert.That(templateResult.ParsingErrors[0].Message, Is.StringContaining("There was no opening tag for the ending tag 'endsomethingelse'"));
48+
//Assert.That
49+
//var result = templateResult.LiquidTemplate.Render(new TemplateContext().WithAllFilters());
50+
51+
// try
52+
// {
53+
//RenderingHelper.RenderTemplate(
54+
//"Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}", templateContext);
55+
// Assert.Fail("This should have thrown an error.");
56+
//}
57+
//catch (LiquidParserException ex)
58+
//{
59+
// var allErrors = String.Join(",", ex.LiquidErrors.Select(x => x.ToString()));
60+
// Logger.Log(allErrors);
61+
// Assert.That(allErrors, Is.StringContaining("There was no opening tag for the ending tag 'endsomethingelse'"));
62+
//}
5663
}
5764

5865

Liquid.NET/src/LiquidASTGenerator.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
8686
parser.AddErrorListener(CreateLiquidErrorListener());
8787
new ParseTreeWalker().Walk(this, parser.init());
8888
}
89+
catch (LiquidParserException lpe)
90+
{
91+
// exceptions thrown when building the AST.
92+
// (don't bother with the re-parse)
93+
foreach (var err in lpe.LiquidErrors)
94+
{
95+
LiquidErrors.Add(err);
96+
}
97+
}
8998
catch
9099
{
91100
parser.Reset();
@@ -106,16 +115,7 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
106115
{
107116
onParserError(err);
108117
}
109-
110-
// TODO: Need to fix the erroring include renderer....
111-
112-
// if (LiquidErrors.Any())
113-
// {
114-
// //ParserError
115-
//
116-
// throw new LiquidParserException(LiquidErrors);
117-
// }
118-
118+
119119
return liquidAst;
120120
}
121121

@@ -831,7 +831,6 @@ public override void ExitCustom_tag_end(LiquidParser.Custom_tag_endContext conte
831831
// then place all the liquid blocks that were siblings in between the start tag and here
832832
// into the new CustomBlockTag
833833

834-
// Log("NEED TO Restructure TREE HERE");
835834
//Log("-- END OF CLOSING CUSTOM TAG: " + context.ENDLABEL());
836835
while(true)
837836
{
@@ -862,21 +861,23 @@ public override void ExitCustom_tag_end(LiquidParser.Custom_tag_endContext conte
862861

863862
break;
864863
}
865-
else
866-
{
867-
//Log("Not a match");
868-
}
864+
// else
865+
// {
866+
// //Log("Not a match");
867+
// }
869868
}
870869
else
871-
{
872-
throw new LiquidParserException(new List<LiquidError> {
873-
new LiquidError
870+
{
871+
var err = new LiquidError
874872
{
875873
Line = context.Start.Line,
876874
CharPositionInLine = context.Start.Column,
877-
Message = "There was no opening tag for the ending tag '" + context.ENDLABEL()+"'"
878-
}
879-
});
875+
Message = "There was no opening tag for the ending tag '" + context.ENDLABEL() + "'"
876+
};
877+
878+
//LiquidErrors.Add(err);
879+
throw new LiquidParserException(new List<LiquidError>{err});
880+
880881

881882
}
882883
}

Liquid.NET/src/LiquidParserException.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
namespace Liquid.NET
66
{
7-
public class LiquidParserException : Exception
7+
/// <summary>
8+
/// Communicate parsing exceptions that are created from
9+
/// additional logic in the AST generator. These should
10+
/// only appear internally.
11+
/// </summary>
12+
internal class LiquidParserException : Exception
813
{
914
private readonly IList<LiquidError> _liquidErrors;
1015

@@ -16,12 +21,13 @@ public override string Message
1621
}
1722
}
1823

19-
public LiquidParserException(IList<LiquidError> liquidErrors)
24+
internal LiquidParserException(IList<LiquidError> liquidErrors)
2025
{
2126
_liquidErrors = liquidErrors;
2227
}
2328

24-
public IList<LiquidError> LiquidErrors {
29+
internal IList<LiquidError> LiquidErrors
30+
{
2531
get { return _liquidErrors; }
2632
}
2733

Liquid.Ruby/tests.liquid

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace Liquid.NET.Tests.Ruby
4747

4848
{% if exceptions != empty %}[Test]{% for test in exceptions %}
4949
[TestCase(@"{{test.input}}", @"{{test.assigns}}", @"{{test.expected | remove: 'EXCEPTION: '}}")]{% endfor %}
50-
public void It_Should_Generate_An_Exception(String input, String assigns, String expectedMessage)
50+
public void It_Should_Capture_An_Error(String input, String assigns, String expectedMessage)
5151
{
5252
// Arrange
5353
ITemplateContext ctx = new TemplateContext()
@@ -61,17 +61,20 @@ namespace Liquid.NET.Tests.Ruby
6161

6262
var template = LiquidTemplate.Create(input);
6363
IList<LiquidError> errors = new List<LiquidError>();
64-
try
65-
{
66-
String result = template.LiquidTemplate.Render(ctx, onRenderingError: errors.Add);
67-
Assert.That(errors.Count, Is.EqualTo(1));
68-
Assert.That(errors[0].ToString(), Is.StringContaining(expectedMessage));
69-
}
70-
catch (LiquidParserException ex)
71-
{
64+
//try
65+
//{
66+
67+
String result = template.LiquidTemplate.Render(ctx, onRenderingError: errors.Add);
68+
Assert.That(errors.Count, Is.EqualTo(1));
69+
Assert.That(errors[0].ToString(), Is.StringContaining(expectedMessage));
70+
71+
// TODO: Clean this up:
72+
//}
73+
//catch (LiquidParserException ex)
74+
//{
7275
// Assert
73-
Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
74-
}
76+
// Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
77+
//}
7578
//catch (LiquidRendererException ex)
7679
//{
7780
// Assert

0 commit comments

Comments
 (0)