diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 4334240278..f633781513 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -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'; @@ -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); } } diff --git a/test/scripts/extend/tag_errors.js b/test/scripts/extend/tag_errors.js index 1b616f2cfc..6a674430b8 100644 --- a/test/scripts/extend/tag_errors.js +++ b/test/scripts/extend/tag_errors.js @@ -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); + } + }); });