Skip to content

Commit 15e8337

Browse files
committed
fix(@angular-devkit/schematics): include the template path in errors thrown by content templates
Rethrow errors thrown while applying a content template, such as a `ReferenceError` from a misspelled variable or a `SyntaxError` inside `<% %>`, as `Failed to apply template "<path>": <error>` and keep the original error as `cause`. The message did not say which template failed, and the ng CLI printed only `foobat is not defined`. Closes #13192
1 parent cca5c3b commit 15e8337

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎packages/angular_devkit/schematics/src/rules/template.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ export function applyContentTemplate<T>(options: T): FileOperator {
7070
throw e;
7171
}
7272

73-
return {
74-
path,
75-
content: Buffer.from(templateImpl(decodedContent, {})(options)),
76-
};
73+
try {
74+
return {
75+
path,
76+
content: Buffer.from(templateImpl(decodedContent, {})(options)),
77+
};
78+
} catch (e) {
79+
throw new Error(`Failed to apply template "${path}": ${String(e)}`, { cause: e });
80+
}
7781
};
7882
}
7983

‎packages/angular_devkit/schematics/src/rules/template_spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ describe('contentTemplate', () => {
183183

184184
expect(applyContentTemplate({})(entry)).toBe(entry);
185185
});
186+
187+
it('names the template in errors thrown while applying it', () => {
188+
const operator = applyContentTemplate({ foobar: 1 });
189+
190+
expect(() => operator(_entry('/a/b/file.ts', 'a<%= foobat %>b'))).toThrowError(
191+
'Failed to apply template "/a/b/file.ts": ReferenceError: foobat is not defined',
192+
);
193+
expect(() => operator(_entry('/a/b/file.ts', 'a<%= foobar unintended %>b'))).toThrowError(
194+
/^Failed to apply template "\/a\/b\/file\.ts": SyntaxError: /,
195+
);
196+
});
197+
198+
it('keeps the original template error as the cause', () => {
199+
expect(() =>
200+
applyContentTemplate({})(_entry('/a/b/file.ts', 'a<%= foobat %>b')),
201+
).toThrowMatching((e) => e.cause instanceof ReferenceError);
202+
});
186203
});
187204

188205
describe('applyTemplateFiles', () => {
@@ -212,4 +229,17 @@ describe('applyTemplateFiles', () => {
212229
]);
213230
expect(tree.readContent('/a/b/file3')).toBe('hello 1 world');
214231
});
232+
233+
it('names the template file in errors', async () => {
234+
const tree = new UnitTestTree(new HostTree());
235+
tree.create('a/b/file__a__.template', 'hello <%= a.missing.length %> world');
236+
237+
const context: SchematicContext = {
238+
strategy: MergeStrategy.Default,
239+
} as SchematicContext;
240+
241+
await expectAsync(
242+
callRule(applyTemplates({ a: 'foo' }), observableOf(tree), context).toPromise(),
243+
).toBeRejectedWithError(/^Failed to apply template "\/a\/b\/file__a__\.template": TypeError: /);
244+
});
215245
});

0 commit comments

Comments
 (0)