Skip to content

Commit

Permalink
[Fleet] Improve error message for bad handlebar template (#119905)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 30, 2021
1 parent 43729d9 commit 13e0310
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 15 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,19 @@ my-package:
const output = compileTemplate(vars, stringTemplate);
expect(output).toEqual(targetOutput);
});

it('should throw on invalid handlebar template', () => {
const streamTemplate = `
input: log
paths:
{{ if test}}
- {{ test}}
{{ end }}
`;
const vars = {};

expect(() => compileTemplate(vars, streamTemplate)).toThrowError(
'Error while compiling agent template: options.inverse is not a function'
);
});
});
10 changes: 8 additions & 2 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ const handlebars = Handlebars.create();

export function compileTemplate(variables: PackagePolicyConfigRecord, templateStr: string) {
const { vars, yamlValues } = buildTemplateVariables(variables, templateStr);
const template = handlebars.compile(templateStr, { noEscape: true });
let compiledTemplate = template(vars);
let compiledTemplate: string;
try {
const template = handlebars.compile(templateStr, { noEscape: true });
compiledTemplate = template(vars);
} catch (err) {
throw new Error(`Error while compiling agent template: ${err.message}`);
}

compiledTemplate = replaceRootLevelYamlVariables(yamlValues, compiledTemplate);

const yamlFromCompiledTemplate = safeLoad(compiledTemplate, {});
Expand Down

0 comments on commit 13e0310

Please sign in to comment.