Skip to content

Commit

Permalink
Make sure module config loading errors have file positioning info
Browse files Browse the repository at this point in the history
Fixes #8845
  • Loading branch information
bep committed Aug 3, 2021
1 parent 9ff17c3 commit d70c485
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
11 changes: 11 additions & 0 deletions common/herrors/error_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion config/configLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"path/filepath"
"strings"

"github.com/gohugoio/hugo/common/herrors"

"github.com/pkg/errors"

"github.com/gohugoio/hugo/common/paths"
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 1 addition & 7 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion modules/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit d70c485

Please sign in to comment.