Skip to content

Commit e50e87e

Browse files
authored
fix(TRAC-875): preserve ValidationError type in render/renderString (#404)
1 parent 0b2b832 commit e50e87e

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ class HandlebarsRenderer {
324324
try {
325325
result = template(context);
326326
} catch(e) {
327+
if (e instanceof ValidationError) {
328+
return reject(new ValidationError(`${e.message} : ${e.stack}`));
329+
}
327330
return reject(new RenderError(`${e.message} : ${e.stack}`));
328331
}
329332

@@ -376,6 +379,9 @@ class HandlebarsRenderer {
376379
try {
377380
result = template(context);
378381
} catch(e) {
382+
if (e instanceof ValidationError) {
383+
return reject(new ValidationError(`${e.message} : ${e.stack}`));
384+
}
379385
return reject(new RenderError(`${e.message} : ${e.stack}`));
380386
}
381387

spec/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ describe('render', () => {
233233
'capitalize_foo': '{{capitalize bar}}',
234234
'with_locale': '{{locale_name}}',
235235
'with_template': '{{template}}',
236+
'assign_var_too_long': '{{assignVar "foo" longString}}',
236237
};
237238
const context = {
238239
bar: 'baz'
@@ -325,6 +326,16 @@ describe('render', () => {
325326
done();
326327
});
327328
});
329+
330+
it('throws ValidationError (not RenderError) if a helper rejects its input, formatted like RenderError', done => {
331+
renderer.render('assign_var_too_long', { longString: 'a'.repeat(1024) }).catch(e => {
332+
expect(e instanceof HandlebarsRenderer.errors.ValidationError).to.be.true();
333+
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.false();
334+
expect(e.message).to.include('assignVar helper value must be less than 1024 characters');
335+
expect(e.message).to.include(' : ');
336+
done();
337+
});
338+
});
328339
});
329340

330341
describe('renderString', () => {
@@ -378,6 +389,16 @@ describe('renderString', () => {
378389
done();
379390
});
380391
});
392+
393+
it('throws ValidationError (not RenderError) if a helper rejects its input, formatted like RenderError', done => {
394+
renderer.renderString('{{assignVar "foo" longString}}', { longString: 'a'.repeat(1024) }).catch(e => {
395+
expect(e instanceof HandlebarsRenderer.errors.ValidationError).to.be.true();
396+
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.false();
397+
expect(e.message).to.include('assignVar helper value must be less than 1024 characters');
398+
expect(e.message).to.include(' : ');
399+
done();
400+
});
401+
});
381402
});
382403

383404
describe('errors', () => {

0 commit comments

Comments
 (0)