The purpose of this module is to parse the Handlebars compile exceptions messages into a more useable format which can be used in handlebars linter(s). Handlebars throws different exceptions depending the template error.
Handlebars exception types:
- Parser Error: handlebars can't compile the template.
- Block Error: the open and close block helpers do not match.
This module parses the exception message into:
{
minLine: 1,
minColumn: 3,
maxLine: 1,
maxColumn: 4,
message: 'foo doesn\'t match bar'
}
npm install handlebars-error-parser --save
In Node.js:
var parser = require('handlebars-error-parser').parser;
var html = '{{#foo}}{{/bar}}';
var parsed;
try {
hbs.precompile(html);
} catch (e) {
parsed = parser(e, html);
}
In browser:
<script src="./node_modules/handlebars-error-parser/index.js"></script>
var html = '{{#foo}}{{/bar}}';
var parsed;
try {
hbs.precompile(html);
} catch (e) {
parsed = window.HandlebarsErrorParser(e, html);
}
foo doesn't match bar - 1:3
Parse error on line 1:
{{foo}}{{/foo}}
-------^
Expecting 'EOF', got 'OPEN_ENDBLOCK'
Parse error on line 1:
{{foo
--^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'
Parse error on line 1:
{{#foo
---^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'