From 8c2c809e5a5d0841dc27aac31e5231b199d74968 Mon Sep 17 00:00:00 2001 From: Gali Ainouz Baum Date: Wed, 8 May 2024 19:39:58 +0300 Subject: [PATCH] feat: catch errors of wrong use of handlebars (#5527) NV-3772 * feat: catch errors of wrong use of handlebars * feat: use type guard for error --- .../compile-template.usecase.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) 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, "'"); }