tpl: Add proper file context to template parse errors

Fixes #13604
This commit is contained in:
Bjørn Erik Pedersen
2025-04-14 11:20:36 +02:00
parent 1e0287f472
commit 8a2830f2dc
3 changed files with 60 additions and 16 deletions

View File

@@ -17,6 +17,7 @@ import (
"path/filepath"
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
)
@@ -229,3 +230,35 @@ layouts/section/section.html
b := hugolib.Test(t, files)
b.AssertFileContent("public/mysection/index.html", "layouts/section/section.html")
}
func TestErrorMessageParseError(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- layouts/home.html --
Line 1.
Line 2. {{ foo }} <- this func does not exist.
Line 3.
`
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`"/layouts/home.html:2:1": parse of template failed: template: home.html:2: function "foo" not defined`))
}
func TestErrorMessageExecuteError(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- layouts/home.html --
Line 1.
Line 2. {{ .Foo }} <- this method does not exist.
Line 3.
`
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, filepath.FromSlash(` "/layouts/home.html:2:11": execute of template failed`))
}