Repository navigation
Expose error location on exceptions and show the source line with a caret - #681
Merged
Merged
Conversation
…aret
HandlebarsException gains public Line and Column properties (zero-based,
same values as the existing "Occured at: line:column" text; null when
the location is unknown).
When a compile-time error has a location, the message now ends with the
template line containing the error and a caret under the column:
Unexpected '!File' inside an expression: ...
Occured at: 0:9
<p>{{#if !File}}yes{{/if}}</p>
^
ExtendedStringReader keeps the text it has read, so the line is
available even when the error is detected after the reader has moved
on (e.g. an unclosed block reported at end of template). The hint is
attached in the compile entry points (Compile and CompileView), shows
only the physical line regardless of line-ending style, keeps tabs
aligned, and cuts very long lines to a window around the column.
Exception types, constructors and the existing message prefix are
unchanged.
Refs #314
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Refs #314
Follow-up to #680, as requested on #314.
What's new
HandlebarsException.Line/HandlebarsException.Columnare new publicint?properties. They're inherited byHandlebarsCompilerException,HandlebarsParserExceptionandHandlebarsRuntimeException.Occured at: line:columntext, so the two always agree.nullwhen the location isn't known. That includes exceptions created through the public constructors.Source hint in the message. When a compile-time error has a location, the message now ends with the template line containing the error and a caret under the column:
Multi-line template, where the error is detected at the stray closing tag on line 3:
How
ExtendedStringReaderkeeps the text it has read and where each line starts. That way the line is still available when an error is detected long after the reader passed it, e.g. an unclosed block reported at the end of the template. If the error's line hasn't been fully read yet, it reads ahead to finish the line, capped at 4096 characters.HandlebarsCompiler.CompileandCompileViewcatchHandlebarsException, attach the hint, and rethrow withthrow;, so the stack trace is unchanged. The hint goes onto the exception and onto anyHandlebarsExceptionit wraps. An exception that already has a hint, e.g. one from a nested layout, keeps the hint for its own source.Messageis overridden so that it, andToString(), include the hint.Environment.NewLine....marking the cut.Compatibility
Messageoverride. Exception types, constructors and the existing message text (includingOccured at: l:c) are the same. The hint is only appended after them, and only for errors that already reported a location.Messageof a located compile error for exact equality will see the extra lines. Matching on type, prefix orContainskeeps working.Tests
New tests in
ExceptionTestscover:\nand\r\nline endingsThere's also a view test in
ViewEngineTests. The full suite passes (2234 tests).🤖 Generated with Claude Code
Closes #314