Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine
MIT License
1.28k stars 219 forks source link

Please report betters errors (context) when having mistakes in the template #314

Open 304NotModified opened 5 years ago

304NotModified commented 5 years ago

Example 1

{{#File}} (mistake, should be {{File}}

gives:

HandlebarsDotNet.HandlebarsCompilerException: Reached end of template before block expression '' was closed
    at HandlebarsDotNet.Compiler.BlockAccumulator.AccumulateBlock(IEnumerator`1 enumerator, BlockAccumulatorContext context)
   at HandlebarsDotNet.Compiler.BlockAccumulator.<ConvertTokens>d__3.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.BlockAccumulator.Accumulate(IEnumerable`1 tokens, HandlebarsConfiguration configuration)
   at HandlebarsDotNet.Compiler.ExpressionBuilder.ConvertTokensToExpressions(IEnumerable`1 tokens)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)
   at HandlebarsDotNet.Handlebars.Compile(String template)

Example 2

{{File}}}

HandlebarsDotNet.HandlebarsCompilerException: Starting and ending handlebars do not match
    at HandlebarsDotNet.Compiler.ExpressionScopeConverter.<ConvertTokens>d__2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.ExpressionScopeConverter.Convert(IEnumerable`1 sequence)
   at HandlebarsDotNet.Compiler.ExpressionBuilder.ConvertTokensToExpressions(IEnumerable`1 tokens)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)
   at HandlebarsDotNet.Handlebars.Compile(String template)

Missing line / column numbers

Example 3

{{#if !File}} {{/if}} (wrong !)

HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> HandlebarsDotNet.HandlebarsCompilerException: An unhandled exception occurred while trying to compile the template ---> System.ArgumentException: must be reducible node
    at System.Linq.Expressions.Expression.VisitChildren(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.VisitExtension(Expression node)
   at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)
   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.VisitBoolishExpression(BoolishExpression bex)
   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)
   at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
   at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)
   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)
   at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
   at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor)
   at HandlebarsDotNet.Compiler.HandlebarsExpressionVisitor.Visit(Expression exp)
   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, Expression parentContext, String templatePath)
--- End of inner exception stack trace ---
    at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, Expression parentContext, String templatePath)
   at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, String templatePath)
--- End of inner exception stack trace ---
    at HandlebarsDotNet.Compiler.FunctionBuilder.Compile(IEnumerable`1 expressions, String templatePath)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)
   at HandlebarsDotNet.Handlebars.Compile(String template)
304NotModified commented 5 years ago

Another example is here: https://github.com/rexm/Handlebars.Net/issues/301

304NotModified commented 5 years ago

Related error issue here: https://github.com/rexm/Handlebars.Net/issues/306

rexm commented 5 years ago

PR welcome

304NotModified commented 5 years ago

Are the row and column numbers tracked after compiling currently? Where could I find them?

rexm commented 5 years ago

They are not. This has been on the roadmap for v2 for a few years and requires a refactoring at every layer in the compiler - no small amount of work, but fairly well understood.

304NotModified commented 5 years ago

@rexm can I go from Token to the original source? If not, could I add that? (maybe missing something from the lexer?)

rexm commented 5 years ago

That would be the first step, each token would need to be reversible. In some cases during that stage some meaningless white space is thrown away so that would need to be accounted for.

304NotModified commented 5 years ago

OK, for error messages the meaningless white space isn't really a problem I guess ;)

304NotModified commented 5 years ago

That would be the first step, each token would need to be reversible. In some cases during that stage some meaningless white space is thrown away so that would need to be accounted for.

something like this? https://github.com/rexm/Handlebars.Net/pull/316

rexm commented 5 years ago

I mention it because if your goal is reporting the row and column, it may be slightly off based on that.

304NotModified commented 5 years ago

yes, row and column would be great, but I think that's the hard for now.

So therefor im also fine with printing the value which is error.

e.g.

{{#File}} could give, {{#File}} is not closed

Luckily the title of this issue is "Please report betters errors " ;)

statler commented 4 years ago

Just following on from this @304NotModified or @rexm what is the best context that can be easily identified to provide more informative feedback to users. In my case, I have users defining their own templates which are later used in the system to generate HTML for fields. When the template is wrong, I would like to provide as much information as I can to help pinpoint the issue rather than something like "Starting and ending handlebars do not match".

I understand that precision will require significant re-engineering, but am curious what could be done given current architecture.