Repository navigation
Closed
Closed
Copy link
Milestone
Description
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)
Metadata
Metadata
Assignees
Labels
No labels
Activity
304NotModified commentedon Aug 5, 2019
Another example is here: #301
304NotModified commentedon Aug 5, 2019
Related error issue here: #306
rexm commentedon Aug 5, 2019
PR welcome
304NotModified commentedon Aug 5, 2019
Are the row and column numbers tracked after compiling currently? Where could I find them?
rexm commentedon Aug 5, 2019
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 commentedon Aug 5, 2019
@rexm can I go from
Tokento the original source? If not, could I add that? (maybe missing something from the lexer?)rexm commentedon Aug 5, 2019
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 commentedon Aug 5, 2019
OK, for error messages the meaningless white space isn't really a problem I guess ;)
304NotModified commentedon Aug 5, 2019
something like this? #316
rexm commentedon Aug 5, 2019
I mention it because if your goal is reporting the row and column, it may be slightly off based on that.
304NotModified commentedon Aug 5, 2019
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 closedLuckily the title of this issue is "Please report betters errors " ;)
statler commentedon Jul 27, 2020
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.
rexm commentedon Nov 8, 2026
/harness adopt
stencil-harness commentedon Nov 8, 2026
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.stencil-harness commentedon Nov 8, 2026
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):
{{#if !File}}(and!as any helper, hash, partial orelse ifargument) now throwsUnexpected '!File' inside an expression: '!' can only start a comment ... and is not a negation operator. Use '#unless' to negate a conditionwith its location. Before, you got a doubly-wrapped "must be reducible node". These templates never compiled before either, so no working template is affected.{{File}}}/{{{File}}now also say which braces opened and closed the expression (the location was already included).{{#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
ExceptionTestscovering 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}}, andFileisn't a registered block helper) still compiles and rendersFile'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.
rexm commentedon Nov 11, 2026
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.
stencil-harness commentedon Nov 11, 2026
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.
stencil-harness commentedon Nov 11, 2026
Handlebars-Net/Handlebars.Net: opened #681, with auto-merge requested once the required checks pass. Nothing else changed.
What changed:
HandlebarsException.LineandHandlebarsException.Column(int?). All three exception types inherit them (compiler, parser, runtime). They're zero-based and hold the same values as the existingOccured at: line:columntext. They'renullwhen the location isn't known.^under the column:CompileandCompileView, 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
Messagefor 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
ExceptionTestsandViewEngineTestscovering the properties, caret position, multi-line templates,\n/\r\nline 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 atnumbers in existing messages, so I didn't do it here.rexm commentedon Nov 11, 2026
I don't like the line:col format, I'd prefer "Line n, column n."
stencil-harness commentedon Nov 11, 2026
Understood: I'll replace the
Occured at: line:coltext in error messages withLine n, column n.and update #681 to match.stencil-harness commentedon Nov 11, 2026
I'm unable to proceed further on this and am awaiting follow-up from a human maintainer.
stencil-harness commentedon Nov 11, 2026
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 ofOccured at: n:n:The numbers themselves are unchanged: zero-based, and equal to
HandlebarsException.Line/Column.How I verified it: updated the
ExceptionTestsassertions to the new format. The full suite passes (2234 tests).Note: code that searched messages for
Occured at:will no longer find it. TheLine/Columnproperties 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.feat: report error location as "Line n, column n."
fix: report token and location for common template syntax errors
feat: expose error location on exceptions and show source line with c…