Skip to content

Commit 0637adb

Browse files
committed
Improve error messages for template failures
1 parent 0bf6135 commit 0637adb

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

���resources/resource_transformers/cssjs/postcss_integration_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestTransformPostCSSError(t *testing.T) {
164164
}).BuildE()
165165

166166
ferrs := herrors.UnwrapFileErrors(err)
167-
b.Assert(len(ferrs), qt.Equals, 1)
167+
b.Assert(len(ferrs), qt.Equals, 2)
168168
b.Assert(err.Error(), qt.Contains, "a.css:4:2")
169169
}
170170

‎tpl/tplimpl/templatestore.go‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -907,52 +907,57 @@ func (s *TemplateStore) addFileContext(ti *TemplInfo, what string, inerr error)
907907

908908
identifiers := s.extractIdentifiers(inerr.Error())
909909

910-
checkFilename := func(fi hugofs.FileMetaInfo, inErr error) (error, bool) {
910+
checkFilename := func(fi hugofs.FileMetaInfo, inErr error) (int, error) {
911+
var matchWeight int
911912
lineMatcher := func(m herrors.LineMatcher) int {
912913
if m.Position.LineNumber != m.LineNumber {
913914
return -1
914915
}
915916

917+
matchWeight++
916918
for _, id := range identifiers {
917919
if strings.Contains(m.Line, id) {
918920
// We found the line, but return a 0 to signal to
919921
// use the column from the error message.
922+
matchWeight++
920923
return 0
921924
}
922925
}
923-
return -1
926+
return 0
924927
}
925928

926929
f, err := fi.Meta().Open()
927930
if err != nil {
928-
return inErr, false
931+
return -1, inErr
929932
}
930933
defer f.Close()
931934

932935
fe := herrors.NewFileErrorFromName(inErr, fi.Meta().Filename)
933936
fe.UpdateContent(f, lineMatcher)
934937

935-
return fe, fe.ErrorContext().Position.IsValid()
938+
return matchWeight, fe
936939
}
937940

938941
inerr = fmt.Errorf("%s: %w", what, inerr)
939942

940943
var (
941-
currentErr error
942-
ok bool
944+
err1 error
945+
weight1 int
946+
err2 error
947+
weight2 int
943948
)
944949

945-
if currentErr, ok = checkFilename(ti.Fi, inerr); ok {
946-
return currentErr
947-
}
950+
weight1, err1 = checkFilename(ti.Fi, inerr)
948951

949952
if ti.base != nil {
950-
if currentErr, ok = checkFilename(ti.base.Fi, inerr); ok {
951-
return currentErr
952-
}
953+
weight2, err2 = checkFilename(ti.base.Fi, inerr)
954+
}
955+
956+
if err2 != nil && weight2 > weight1 {
957+
return err2
953958
}
954959

955-
return currentErr
960+
return err1
956961
}
957962

958963
func (s *TemplateStore) extractIdentifiers(line string) []string {

0 commit comments

Comments
 (0)