"path/filepath"
"testing"
+ qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
)
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`))
+}
"_shortcodes/twitter.html": {"_shortcodes/tweet.html"},
}
-func (t *templateNamespace) parseTemplate(ti *TemplInfo) error {
+func (s *TemplateStore) parseTemplate(ti *TemplInfo) error {
+ err := s.tns.doParseTemplate(ti)
+ if err != nil {
+ return s.addFileContext(ti, "parse of template failed", err)
+ }
+
+ return err
+}
+
+func (t *templateNamespace) doParseTemplate(ti *TemplInfo) error {
if !ti.noBaseOf || ti.category == CategoryBaseof {
// Delay parsing until we have the base template.
return nil
execErr := t.storeSite.executer.ExecuteWithContext(ctx, ti, wr, data)
if execErr != nil {
- return t.addFileContext(ti, execErr)
+ return t.addFileContext(ti, "execute of template failed", execErr)
}
return nil
}
return nil
}
-func (s *TemplateStore) addFileContext(ti *TemplInfo, inerr error) error {
+func (s *TemplateStore) addFileContext(ti *TemplInfo, what string, inerr error) error {
if ti.Fi == nil {
return inerr
}
fe := herrors.NewFileErrorFromName(inErr, fi.Meta().Filename)
fe.UpdateContent(f, lineMatcher)
- if !fe.ErrorContext().Position.IsValid() {
- return inErr, false
- }
- return fe, true
+ return fe, fe.ErrorContext().Position.IsValid()
}
- inerr = fmt.Errorf("execute of template failed: %w", inerr)
+ inerr = fmt.Errorf("%s: %w", what, inerr)
- if err, ok := checkFilename(ti.Fi, inerr); ok {
- return err
+ var (
+ currentErr error
+ ok bool
+ )
+
+ if currentErr, ok = checkFilename(ti.Fi, inerr); ok {
+ return currentErr
}
if ti.base != nil {
- if err, ok := checkFilename(ti.base.Fi, inerr); ok {
- return err
+ if currentErr, ok = checkFilename(ti.base.Fi, inerr); ok {
+ return currentErr
}
}
- return inerr
+ return currentErr
}
func (s *TemplateStore) extractIdentifiers(line string) []string {
if vv.state == processingStateTransformed {
continue
}
- if err := s.tns.parseTemplate(vv); err != nil {
+ if err := s.parseTemplate(vv); err != nil {
return err
}
}
// The regular expression used to detect if a template needs a base template has some
// rare false positives. Assume we don't need one.
vv.noBaseOf = true
- if err := s.tns.parseTemplate(vv); err != nil {
+ if err := s.parseTemplate(vv); err != nil {
return err
}
continue
if vvv.state == processingStateTransformed {
continue
}
- if err := s.tns.parseTemplate(vvv); err != nil {
+ if err := s.parseTemplate(vvv); err != nil {
return err
}
}