Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: catch errors of wrong use of handlebars #5527

Merged
merged 4 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading