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 2 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
Expand Up @@ -5,6 +5,7 @@ import { 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,16 @@ 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: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (e: any) {
} catch (e: unknown) {

Better TS ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it creates a ts error for e.message 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, thats how all catch errors are typed in the project

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ainouzgali You can use checkIsResponseError as a type-guard here! TypeScript began recommending unknown as the type for catch in TS 4.4

Copy link
Contributor

@antonjoel82 antonjoel82 May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BiswaViraj Side note: I would suggest that we reserve Request changes for blocking problems (like issue or important chores) 🙂 -- this is probably more of a nitpick until we put it into a coding style guide

(But agree with commenting on it so we have fewer places to change later 🙌 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Didn't know about it. Thanks!

throw new ApiException(
e?.message || `Message content could not be generated`
);
}

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