Make sure module config loading errors have file positioning info
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 3 Aug 2021 07:57:14 +0000 (09:57 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 3 Aug 2021 07:57:14 +0000 (09:57 +0200)
Fixes #8845

common/herrors/error_locator.go
config/configLoader.go
hugolib/config.go
modules/collect.go

index 118ab851cf50495f19efaef9276f970e5300defa..2c0d215b163a4b5df20957072ed5653a92bde4cd 100644 (file)
@@ -100,6 +100,17 @@ func WithFileContextForFile(e error, realFilename, filename string, fs afero.Fs,
        return WithFileContext(e, realFilename, f, matcher)
 }
 
+// WithFileContextForFileDefault tries to add file context using the default line matcher.
+func WithFileContextForFileDefault(err error, filename string, fs afero.Fs) error {
+       err, _ = WithFileContextForFile(
+               err,
+               filename,
+               filename,
+               fs,
+               SimpleLineMatcher)
+       return err
+}
+
 // WithFileContextForFile will try to add a file context with lines matching the given matcher.
 // If no match could be found, the original error is returned with false as the second return value.
 func WithFileContext(e error, realFilename string, r io.Reader, matcher LineMatcherFn) (error, bool) {
index 8dcfcbdcc0cccefeb09f2d8f7ff694e82b9e9190..145b95d7da130cd3be49887d953ce5bb5b26a3af 100644 (file)
@@ -18,6 +18,8 @@ import (
        "path/filepath"
        "strings"
 
+       "github.com/gohugoio/hugo/common/herrors"
+
        "github.com/pkg/errors"
 
        "github.com/gohugoio/hugo/common/paths"
@@ -58,7 +60,7 @@ func FromConfigString(config, configType string) (Provider, error) {
 func FromFile(fs afero.Fs, filename string) (Provider, error) {
        m, err := loadConfigFromFile(fs, filename)
        if err != nil {
-               return nil, err
+               return nil, herrors.WithFileContextForFileDefault(err, filename, fs)
        }
        return NewFrom(m), nil
 }
index 5f07a4571d83a6ca907ece731e443ba75e44819f..945d9eec635defe8cd42523c0d1d0362fe6e3e1b 100644 (file)
@@ -507,11 +507,5 @@ func (configLoader) loadSiteConfig(cfg config.Provider) (scfg SiteConfig, err er
 }
 
 func (l configLoader) wrapFileError(err error, filename string) error {
-       err, _ = herrors.WithFileContextForFile(
-               err,
-               filename,
-               filename,
-               l.Fs,
-               herrors.SimpleLineMatcher)
-       return err
+       return herrors.WithFileContextForFileDefault(err, filename, l.Fs)
 }
index 026373b54574a0a6983b8cd1a07056eb50d43eca..29bf057a684799f64ba4339f89a9209f85968695 100644 (file)
@@ -437,7 +437,7 @@ func (c *collector) applyThemeConfig(tc *moduleAdapter) error {
                        var err error
                        tc.cfg, err = config.FromFile(c.fs, configFilename)
                        if err != nil {
-                               return errors.Wrapf(err, "failed to read module config for %q in %q", tc.Path(), configFilename)
+                               return err
                        }
                }