Skip to content

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

Closed

Description

@304NotModified

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)

Activity

  1. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

    Another example is here: #301

  2. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

    Related error issue here: #306

  3. rexm commented on Aug 5, 2019

    @rexm
    Member

    PR welcome

  4. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

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

  5. rexm commented on Aug 5, 2019

    @rexm
    Member

    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.

  6. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

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

  7. rexm commented on Aug 5, 2019

    @rexm
    Member

    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.

  8. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

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

  9. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

    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? #316

  10. rexm commented on Aug 5, 2019

    @rexm
    Member

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

  11. 304NotModified commented on Aug 5, 2019

    @304NotModified
    ContributorAuthor

    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 " ;)

  12. statler commented on Jul 27, 2020

    @statler

    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.

  13. added this to the v3 milestone on Dec 29, 2021
  14. rexm commented on Nov 8, 2026

    @rexm
    Member

    /harness adopt

  15. stencil-harness commented on Nov 8, 2026

    @stencil-harness
    Contributor

    I'm the agent managing intake of issues on Rex's behalf. I understand the request as improving compiler error messages for malformed templates (unclosed blocks, mismatched braces, invalid expressions like !File) so they include context about the offending expression, ideally with location. I'll evaluate the current behavior and whether I can address this autonomously.

  16. stencil-harness commented on Nov 8, 2026

    @stencil-harness
    Contributor

    Handlebars-Net/Handlebars.Net: opened #680, with auto-merge requested once the required checks pass.

    What changed (exception types are the same, and the old message text is kept at the start of each message):

    • Example 3: {{#if !File}} (and ! as any helper, hash, partial or else if argument) now throws Unexpected '!File' inside an expression: '!' can only start a comment ... and is not a negation operator. Use '#unless' to negate a condition with its location. Before, you got a doubly-wrapped "must be reducible node". These templates never compiled before either, so no working template is affected.
    • Example 2: {{File}}} / {{{File}} now also say which braces opened and closed the expression (the location was already included).
    • Unclosed blocks (e.g. {{#each items}} with no closing tag) and stray closing tags ({{/unless}} with no matching opening tag) now include the location of the tag at fault. The compiler already recorded source positions, but they were dropped partway through compilation; they're now kept.

    How I verified it: added cases to ExceptionTests covering each message and its location, plus checks that normal comments and layouts still compile. The full test suite passes (2224 tests) and the library builds for all its targets.

    Needs a decision: Example 1. An unclosed {{#File}} (no {{/File}}, and File isn't a registered block helper) still compiles and renders File's value as if it were {{File}}. Handlebars.js rejects this template. Making it a compile error would break existing templates that depend on today's behavior, so I left it alone. @rexm, should this become an error, maybe behind a compatibility flag, or stay as it is?

    Note: the PR text says it closes this issue, so merging #680 will close it even though Example 1 is still open. Reopen it if you want to track that decision here.

  17. rexm commented on Nov 11, 2026

    @rexm
    Member

    I'd like to refine the architecture of how we implemented this. If it would not cause any breaking issues, we should introduce either new properties on the exception classes or on child classes to include the position (line/col) as well as a separate string hint in the full exception message which shows the full original line the error sits on, and a carat underneath the column where the error begins.

  18. reopened this on Nov 11, 2026
  19. stencil-harness commented on Nov 11, 2026

    @stencil-harness
    Contributor

    Understood. I'll add public line/column properties to the Handlebars exception types and extend the message with the source line the error is on and a caret under the error column. I'm checking now that this can be done without breaking existing consumers.

  20. stencil-harness commented on Nov 11, 2026

    @stencil-harness
    Contributor

    Handlebars-Net/Handlebars.Net: opened #681, with auto-merge requested once the required checks pass. Nothing else changed.

    What changed:

    • New properties HandlebarsException.Line and HandlebarsException.Column (int?). All three exception types inherit them (compiler, parser, runtime). They're zero-based and hold the same values as the existing Occured at: line:column text. They're null when the location isn't known.
    • Source line with a caret in the message. When a compile error has a location, the message now ends with the template line the error is on and a ^ under the column:
      Occured at: 0:9
      
      <p>{{#if !File}}yes{{/if}}</p>
               ^
      
      This works for Compile and CompileView, including errors detected long after the line was read (e.g. an unclosed block reported at the end of the template). Only the line with the error is shown, whatever line endings the template uses. Tabs are kept so the caret lines up, and lines over 120 characters are cut to a window around the column.

    Compatibility: no public API was removed or changed, only added to. Exception types, constructors and the existing message text stay the same; the hint is appended only to errors that already reported a location. Code that compares a located error's full Message for exact equality will see the extra lines. Compiling now keeps a copy of the template text until compilation finishes; rendering is unaffected.

    How I verified it: added tests to ExceptionTests and ViewEngineTests covering the properties, caret position, multi-line templates, \n/\r\n line endings, tabs, long lines, parser errors and errors without a location. The full suite passes (2234 tests) and the library builds for all its targets.

    One open choice: lines and columns stay zero-based to match the existing message. If you'd prefer one-based numbers (what editors show), that would change the Occured at numbers in existing messages, so I didn't do it here.

  21. rexm commented on Nov 11, 2026

    @rexm
    Member

    I don't like the line:col format, I'd prefer "Line n, column n."

  22. stencil-harness commented on Nov 11, 2026

    @stencil-harness
    Contributor

    Understood: I'll replace the Occured at: line:col text in error messages with Line n, column n. and update #681 to match.

  23. stencil-harness commented on Nov 11, 2026

    @stencil-harness
    Contributor

    I'm unable to proceed further on this and am awaiting follow-up from a human maintainer.

  24. stencil-harness commented on Nov 11, 2026

    @stencil-harness
    Contributor

    Handlebars-Net/Handlebars.Net: opened #682 (#681 had already merged with the old format). Auto-merge is requested, so it will merge once the required checks pass. Nothing else changed.

    What changed: when an error has a location, its message now reports it as Line n, column n. instead of Occured at: n:n:

    Unexpected '!File' inside an expression: ...
    
    Line 0, column 9.
    
    <p>{{#if !File}}yes{{/if}}</p>
             ^
    

    The numbers themselves are unchanged: zero-based, and equal to HandlebarsException.Line / Column.

    How I verified it: updated the ExceptionTests assertions to the new format. The full suite passes (2234 tests).

    Note: code that searched messages for Occured at: will no longer find it. The Line/Column properties are the reliable way to read the position. Since the reported numbers are now spelled out as "Line 0", let me know if you'd like them one-based.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions