Skip to content

Commit

Permalink
feat: catch errors of wrong use of handlebars (#5527) NV-3772
Browse files Browse the repository at this point in the history
* feat: catch errors of wrong use of handlebars

* feat: use type guard for error
  • Loading branch information
ainouzgali authored May 8, 2024
1 parent 4286c66 commit 8c2c809
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Injectable } from '@nestjs/common';
import * as Handlebars from 'handlebars';
import { format } from 'date-fns';
import { HandlebarHelpersEnum } from '@novu/shared';
import { checkIsResponseError, HandlebarHelpersEnum } from '@novu/shared';

import { CompileTemplateCommand } from './compile-template.command';
import * as i18next from 'i18next';
import { ApiException } from '../../utils/exceptions';

const assertResult = (condition: boolean, options) => {
const fn = condition ? options.fn : options.inverse;
Expand Down Expand Up @@ -208,10 +209,18 @@ Handlebars.registerHelper(
export class CompileTemplate {
async execute(command: CompileTemplateCommand): Promise<string> {
const templateContent = command.template;

const template = Handlebars.compile(templateContent);

const result = template(command.data, {});
let result = '';
try {
const template = Handlebars.compile(templateContent);

result = template(command.data, {});
} catch (e: unknown) {
let errorMessage = `Message content could not be generated`;
if (checkIsResponseError(e)) {
errorMessage = e.message;
}
throw new ApiException(errorMessage);
}

return result.replace(/&#x27;/g, "'");
}
Expand Down

0 comments on commit 8c2c809

Please sign in to comment.