diff --git a/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts b/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts index 3238ecf1759..bc4370c0812 100644 --- a/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts +++ b/packages/application-generic/src/usecases/compile-template/compile-template.usecase.ts @@ -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; @@ -208,10 +209,18 @@ Handlebars.registerHelper( export class CompileTemplate { async execute(command: CompileTemplateCommand): Promise { 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(/'/g, "'"); }