Skip to content

Commit

Permalink
fix(tag): show source file in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Aug 11, 2022
1 parent 1ee76ce commit fd882ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/extend/tag.js
Expand Up @@ -160,15 +160,15 @@ 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) : '');
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 @@ -245,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
4 changes: 2 additions & 2 deletions test/scripts/extend/tag_errors.js
Expand Up @@ -142,8 +142,8 @@ describe('Tag Errors', () => {
try {
await tag.render(body, { source });
} catch (err) {
err.should.have.property('stack');
err.stack.should.contains(source);
err.should.have.property('message');
err.message.should.contains(source);
}
});
});

0 comments on commit fd882ab

Please sign in to comment.