From bb9d65e95b31f749eaad1dce244dd8d5ab028c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Tue, 15 Feb 2022 16:33:09 +0100 Subject: [PATCH] [IMP] app: better error message when missing template --- src/app/template_set.ts | 9 +++++++-- tests/components/basics.test.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/app/template_set.ts b/src/app/template_set.ts index d39b6973..4c8c13d6 100644 --- a/src/app/template_set.ts +++ b/src/app/template_set.ts @@ -1,6 +1,6 @@ import { createBlock, html, list, multi, text, toggler, comment } from "../blockdom"; import { compile, Template } from "../compiler"; -import { component } from "../component/component_node"; +import { component, getCurrent } from "../component/component_node"; import { UTILS } from "./template_helpers"; const bdom = { text, createBlock, list, multi, html, toggler, component, comment }; @@ -94,7 +94,12 @@ export class TemplateSet { if (!(name in this.templates)) { const rawTemplate = this.rawTemplates[name]; if (rawTemplate === undefined) { - throw new Error(`Missing template: "${name}"`); + let extraInfo = ""; + try { + const componentName = getCurrent().component.constructor.name; + extraInfo = ` (for component "${componentName}")`; + } catch {} + throw new Error(`Missing template: "${name}"${extraInfo}`); } const templateFn = this._compileTemplate(name, rawTemplate); // first add a function to lazily get the template, in case there is a diff --git a/tests/components/basics.test.ts b/tests/components/basics.test.ts index 2b9db3bf..a07ebf42 100644 --- a/tests/components/basics.test.ts +++ b/tests/components/basics.test.ts @@ -204,7 +204,7 @@ describe("basics", () => { error = e as Error; } expect(error!).toBeDefined(); - expect(error!.message).toBe('Missing template: "wrongtemplate"'); + expect(error!.message).toBe('Missing template: "wrongtemplate" (for component "Test")'); }); test("class component with dynamic text", async () => {