Skip to content

Commit

Permalink
fix(tag): show source file in unformatted error message (#5031)
Browse files Browse the repository at this point in the history
* fix(tag): show source file in error stack

* fix(tag): show source file in error message
  • Loading branch information
curbengh authored Aug 13, 2022
1 parent bbf09ac commit b48f095
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ const getContext = (lines, errLine, location, type) => {
* @return {Error} New error object with embedded context
*/
const formatNunjucksError = (err, input, source = '') => {
err.message = err.message.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);
if (isNaN(errLine)) return err;

// trim useless info from Nunjucks Error
const splited = err.message.replace('(unknown path)', source ? magenta(source) : '').split('\n');
const splited = err.message.split('\n');

const e = new Error();
e.name = 'Nunjucks Error';
Expand Down Expand Up @@ -243,7 +245,9 @@ class Tag {
options,
cb
);
}).catch(err => Promise.reject(formatNunjucksError(err, str, source)))
}).catch(err => {
return Promise.reject(formatNunjucksError(err, str, source));
})
.asCallback(callback);
}
}
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('message');
err.message.should.contains(source);
}
});
});

0 comments on commit b48f095

Please sign in to comment.