Skip to content

Commit

Permalink
fix(templates): try/catch handlebars compile (#19794)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
rarkins and viceice committed Jan 14, 2023
1 parent 74e592c commit 98247d3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/util/template/index.spec.ts
Expand Up @@ -15,6 +15,10 @@ describe('util/template/index', () => {
});
});

it('returns empty string if cannot compile', () => {
expect(template.safeCompile('{{abc', {})).toBe('');
});

it('has valid exposed config options', () => {
const allOptions = getOptions().map((option) => option.name);
const missingOptions = template.exposedConfigOptions.filter(
Expand Down
13 changes: 13 additions & 0 deletions lib/util/template/index.ts
Expand Up @@ -231,6 +231,19 @@ export function compile(
return handlebars.compile(template)(filteredInput);
}

export function safeCompile(
template: string,
input: CompileInput,
filterFields = true
): string {
try {
return compile(template, input, filterFields);
} catch (err) {
logger.warn({ err, template }, 'Error compiling template');
return '';
}
}

export function containsTemplates(
value: unknown,
templates: string | string[]
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/update/pr/body/footer.spec.ts
Expand Up @@ -22,7 +22,7 @@ describe('workers/repository/update/pr/body/footer', () => {
});

it('renders prFooter', () => {
template.compile.mockImplementation((x) => x);
template.safeCompile.mockImplementation((x) => x);
expect(
getPrFooter({
manager: 'some-manager',
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/update/pr/body/footer.ts
Expand Up @@ -3,7 +3,7 @@ import type { BranchConfig } from '../../../../types';

export function getPrFooter(config: BranchConfig): string {
if (config.prFooter) {
return '\n---\n\n' + template.compile(config.prFooter, config);
return '\n---\n\n' + template.safeCompile(config.prFooter, config);
}
return '';
}

0 comments on commit 98247d3

Please sign in to comment.