Skip to content

Commit

Permalink
improvement(core): include path in template errors (#5692)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! Here are some tips for you:

1. If this is your first pull request, please read our contributor
guidelines in the
https://github.com/garden-io/garden/blob/main/CONTRIBUTING.md file.
2. Please label this pull request according to what type of issue you
are addressing (see "What type of PR is this?" below)
3. Ensure you have added or run the appropriate tests for your PR.
4. If the PR is unfinished, add `WIP:` at the beginning of the title or
use the GitHub Draft PR feature.
5. Please add at least two reviewers to the PR. Currently active
maintainers are: @edvald, @thsig, @eysi09, @shumailxyz, @stefreak,
@TimBeyer, @mkhq, and @vvagaytsev.
-->

**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.
  • Loading branch information
thsig authored Feb 13, 2024
1 parent b2dddcc commit 5dfb0f7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion core/src/template-string/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
],
})
})
Expand Down
6 changes: 4 additions & 2 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
)
})

Expand Down

0 comments on commit 5dfb0f7

Please sign in to comment.