Skip to content

Commit

Permalink
fix(tag): show source file in error stack
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Aug 10, 2022
1 parent bbf09ac commit 1ee76ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ const getContext = (lines, errLine, location, type) => {
* @return {Error} New error object with embedded context
*/
const formatNunjucksError = (err, input, source = '') => {
err.stack = err.stack.replace('(unknown path)', source ? magenta(source) : '');

const match = err.message.match(/Line (\d+), Column \d+/);
if (!match) return err;
const errLine = parseInt(match[1], 10);
Expand Down
22 changes: 22 additions & 0 deletions test/scripts/extend/tag_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,26 @@ describe('Tag Errors', () => {
err.message.should.contains(source);
}
});

it('source file path 2', async () => {
const source = '_posts/hello-world.md';
const tag = new Tag();

tag.register('test',
(args, content) => {},
{ ends: true });

const body = [
'{% test %}',
'${#var}',
'{% endtest %}'
].join('\n');

try {
await tag.render(body, { source });
} catch (err) {
err.should.have.property('stack');
err.stack.should.contains(source);
}
});
});

0 comments on commit 1ee76ce

Please sign in to comment.