Skip to content

Commit b48f095

Browse files
authored
fix(tag): show source file in unformatted error message (#5031)
* fix(tag): show source file in error stack * fix(tag): show source file in error message
1 parent bbf09ac commit b48f095

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/extend/tag.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ const getContext = (lines, errLine, location, type) => {
160160
* @return {Error} New error object with embedded context
161161
*/
162162
const formatNunjucksError = (err, input, source = '') => {
163+
err.message = err.message.replace('(unknown path)', source ? magenta(source) : '');
164+
163165
const match = err.message.match(/Line (\d+), Column \d+/);
164166
if (!match) return err;
165167
const errLine = parseInt(match[1], 10);
166168
if (isNaN(errLine)) return err;
167169

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

171173
const e = new Error();
172174
e.name = 'Nunjucks Error';
@@ -243,7 +245,9 @@ class Tag {
243245
options,
244246
cb
245247
);
246-
}).catch(err => Promise.reject(formatNunjucksError(err, str, source)))
248+
}).catch(err => {
249+
return Promise.reject(formatNunjucksError(err, str, source));
250+
})
247251
.asCallback(callback);
248252
}
249253
}

test/scripts/extend/tag_errors.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,26 @@ describe('Tag Errors', () => {
124124
err.message.should.contains(source);
125125
}
126126
});
127+
128+
it('source file path 2', async () => {
129+
const source = '_posts/hello-world.md';
130+
const tag = new Tag();
131+
132+
tag.register('test',
133+
(args, content) => {},
134+
{ ends: true });
135+
136+
const body = [
137+
'{% test %}',
138+
'${#var}',
139+
'{% endtest %}'
140+
].join('\n');
141+
142+
try {
143+
await tag.render(body, { source });
144+
} catch (err) {
145+
err.should.have.property('message');
146+
err.message.should.contains(source);
147+
}
148+
});
127149
});

0 commit comments

Comments
 (0)