From 5dfb0f7cb6b698aad4c78530ed225f0a3b09952a Mon Sep 17 00:00:00 2001 From: Thorarinn Sigurdsson Date: Tue, 13 Feb 2024 13:31:10 +0100 Subject: [PATCH] improvement(core): include path in template errors (#5692) **What this PR does / why we need it**: When available, include the path to the template string in the YAML doc in question. Also stop truncating the template expression in the message. --- core/src/template-string/template-string.ts | 5 ++++- core/test/unit/src/garden.ts | 2 +- core/test/unit/src/template-string.ts | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/core/src/template-string/template-string.ts b/core/src/template-string/template-string.ts index 3ee157fc89..3f1aff100a 100644 --- a/core/src/template-string/template-string.ts +++ b/core/src/template-string/template-string.ts @@ -224,7 +224,10 @@ export function resolveTemplateString({ if (!(err instanceof GardenError)) { throw err } - const prefix = `Invalid template string (${styles.accent(truncate(string, 35).replace(/\n/g, "\\n"))}): ` + const pathDescription = path ? ` at path ${styles.accent(path.join("."))}` : "" + const prefix = `Invalid template string (${styles.accent( + truncate(string, 200).replace(/\n/g, "\\n") + )})${pathDescription}: ` const message = err.message.startsWith(prefix) ? err.message : prefix + err.message throw new TemplateStringError({ message, path }) diff --git a/core/test/unit/src/garden.ts b/core/test/unit/src/garden.ts index c0bdd4bc4d..ad9699e6dd 100644 --- a/core/test/unit/src/garden.ts +++ b/core/test/unit/src/garden.ts @@ -3166,7 +3166,7 @@ describe("Garden", () => { await expectError(() => garden.resolveModules({ log: garden.log }), { contains: [ "Failed resolving one or more modules:", - `module-a: Invalid template string (${key}): config module-a cannot reference itself.`, + `module-a: Invalid template string (${key}) at path spec.build.command.0: config module-a cannot reference itself.`, ], }) }) diff --git a/core/test/unit/src/template-string.ts b/core/test/unit/src/template-string.ts index bd86edfe67..9738076d8c 100644 --- a/core/test/unit/src/template-string.ts +++ b/core/test/unit/src/template-string.ts @@ -20,6 +20,7 @@ import { expectError, getDataDir, makeTestGarden } from "../../helpers.js" import { dedent } from "../../../src/util/string.js" import stripAnsi from "strip-ansi" import { TemplateStringError } from "../../../src/exceptions.js" +import repeat from "lodash-es/repeat.js" class TestContext extends ConfigContext { constructor(context) { @@ -157,13 +158,14 @@ describe("resolveTemplateString", () => { }) it("should trim long template string in error messages", () => { + const veryLongString = repeat("very ", 100) void expectError( () => resolveTemplateString({ - string: "${some} very very very very very long long long long long template string", + string: `\${some} ${veryLongString} template string`, context: new TestContext({}), }), - { contains: "Invalid template string (${some} very very very very very l…): Could not find key some." } + (err) => expect(err.message.length).to.be.lessThan(350) ) })