Skip to content

Commit

Permalink
fix: add precompile error for renderString (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored Aug 6, 2024
1 parent 615f7d4 commit 06d3580
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const HandlebarsV4 = require('@bigcommerce/handlebars-v4');
const helpers = require('./helpers');

const AppError = require('./lib/appError');
const { CompileError, FormatError, RenderError, DecoratorError, TemplateNotFoundError, ValidationError } = require('./lib/errors');
const { CompileError, FormatError, RenderError, DecoratorError, TemplateNotFoundError, ValidationError, PrecompileError } = require('./lib/errors');

const handlebarsOptions = {
preventIndent: true
Expand All @@ -23,6 +23,7 @@ class HandlebarsRenderer {
DecoratorError,
TemplateNotFoundError,
ValidationError,
PrecompileError,
};
}

Expand Down Expand Up @@ -335,17 +336,21 @@ class HandlebarsRenderer {
*/
renderString(template, context) {
return new Promise((resolve, reject) => {
let precompiledTemplate;
let precompiled, precompiledTemplate;
context = context || {};

if (typeof template !== 'string') {
return reject(new CompileError('Template must be a string'));
}

// Compile the template
try {
delete this.handlebars.compile;
const precompiled = this.handlebars.precompile(template, handlebarsOptions);
precompiled = this.handlebars.precompile(template, handlebarsOptions);
} catch (e) {
return reject(new PrecompileError(e.message));
}

try {
eval(`precompiledTemplate = ${precompiled}`);
template = this.handlebars.template(precompiledTemplate);
} catch(e) {
Expand Down
4 changes: 3 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class RenderError extends AppError {}; // Error rendering template
class DecoratorError extends AppError {}; // Error applying decorator
class TemplateNotFoundError extends AppError {}; // Template not registered
class ValidationError extends Error {};
class PrecompileError extends Error {};

module.exports = {
CompileError,
FormatError,
RenderError,
DecoratorError,
TemplateNotFoundError,
ValidationError
ValidationError,
PrecompileError
};
4 changes: 2 additions & 2 deletions spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ describe('renderString', () => {
});
});

it('throws RenderError if given malformed template', done => {
it('throws PrecompileError if given malformed template', done => {
renderer.renderString('{{', context).catch(e => {
expect(e instanceof HandlebarsRenderer.errors.CompileError).to.be.true();
expect(e instanceof HandlebarsRenderer.errors.PrecompileError).to.be.true();
done();
});
});
Expand Down

0 comments on commit 06d3580

Please sign in to comment.