Improve error messages, esp. when the server is running

* Add file context to minifier errors when publishing
* Misc fixes (see issues)
* Allow custom server error template in layouts/server/error.html

To get to this, this commit also cleans up and simplifies the code surrounding errors and files. This also removes the usage of `github.com/pkg/errors`, mostly because of https://github.com/pkg/errors/issues/223 -- but also because most of this is now built-in to Go.

Fixes #9852
Fixes #9857
Fixes #9863
This commit is contained in:
Bjørn Erik Pedersen 2022-05-02 16:07:52 +02:00
parent 6eea32bd6b
commit f2946da9e8
109 changed files with 861 additions and 780 deletions

View file

@ -47,7 +47,8 @@ import (
"github.com/gohugoio/hugo/common/hugio"
"github.com/pkg/errors"
"errors"
"github.com/spf13/afero"
)
@ -241,7 +242,7 @@ func (c *Client) Vendor() error {
// See https://github.com/gohugoio/hugo/issues/8239
// This is an error situation. We need something to vendor.
if t.Mounts() == nil {
return errors.Errorf("cannot vendor module %q, need at least one mount", t.Path())
return fmt.Errorf("cannot vendor module %q, need at least one mount", t.Path())
}
fmt.Fprintln(&modulesContent, "# "+t.Path()+" "+t.Version())
@ -253,22 +254,22 @@ func (c *Client) Vendor() error {
targetFilename := filepath.Join(vendorDir, t.Path(), mount.Source)
fi, err := c.fs.Stat(sourceFilename)
if err != nil {
return errors.Wrap(err, "failed to vendor module")
return fmt.Errorf("failed to vendor module: %w", err)
}
if fi.IsDir() {
if err := hugio.CopyDir(c.fs, sourceFilename, targetFilename, nil); err != nil {
return errors.Wrap(err, "failed to copy module to vendor dir")
return fmt.Errorf("failed to copy module to vendor dir: %w", err)
}
} else {
targetDir := filepath.Dir(targetFilename)
if err := c.fs.MkdirAll(targetDir, 0755); err != nil {
return errors.Wrap(err, "failed to make target dir")
return fmt.Errorf("failed to make target dir: %w", err)
}
if err := hugio.CopyFile(c.fs, sourceFilename, targetFilename); err != nil {
return errors.Wrap(err, "failed to copy module file to vendor")
return fmt.Errorf("failed to copy module file to vendor: %w", err)
}
}
}
@ -278,7 +279,7 @@ func (c *Client) Vendor() error {
_, err := c.fs.Stat(resourcesDir)
if err == nil {
if err := hugio.CopyDir(c.fs, resourcesDir, filepath.Join(vendorDir, t.Path(), files.FolderResources), nil); err != nil {
return errors.Wrap(err, "failed to copy resources to vendor dir")
return fmt.Errorf("failed to copy resources to vendor dir: %w", err)
}
}
@ -287,7 +288,7 @@ func (c *Client) Vendor() error {
_, err = c.fs.Stat(configDir)
if err == nil {
if err := hugio.CopyDir(c.fs, configDir, filepath.Join(vendorDir, t.Path(), "config"), nil); err != nil {
return errors.Wrap(err, "failed to copy config dir to vendor dir")
return fmt.Errorf("failed to copy config dir to vendor dir: %w", err)
}
}
@ -361,7 +362,7 @@ func (c *Client) get(args ...string) error {
args = append([]string{"-d"}, args...)
}
if err := c.runGo(context.Background(), c.logger.Out(), append([]string{"get"}, args...)...); err != nil {
errors.Wrapf(err, "failed to get %q", args)
return fmt.Errorf("failed to get %q: %w", args, err)
}
return nil
}
@ -372,7 +373,7 @@ func (c *Client) get(args ...string) error {
func (c *Client) Init(path string) error {
err := c.runGo(context.Background(), c.logger.Out(), "mod", "init", path)
if err != nil {
return errors.Wrap(err, "failed to init modules")
return fmt.Errorf("failed to init modules: %w", err)
}
c.GoModulesFilename = filepath.Join(c.ccfg.WorkingDir, goModFilename)
@ -458,7 +459,7 @@ func (c *Client) listGoMods() (goModules, error) {
out := ioutil.Discard
err := c.runGo(context.Background(), out, args...)
if err != nil {
return errors.Wrap(err, "failed to download modules")
return fmt.Errorf("failed to download modules: %w", err)
}
return nil
}
@ -477,7 +478,7 @@ func (c *Client) listGoMods() (goModules, error) {
}
err := c.runGo(context.Background(), b, args...)
if err != nil {
return errors.Wrap(err, "failed to list modules")
return fmt.Errorf("failed to list modules: %w", err)
}
dec := json.NewDecoder(b)
@ -487,7 +488,7 @@ func (c *Client) listGoMods() (goModules, error) {
if err == io.EOF {
break
}
return errors.Wrap(err, "failed to decode modules list")
return fmt.Errorf("failed to decode modules list: %w", err)
}
if err := handle(m); err != nil {
@ -657,7 +658,7 @@ If you then run 'hugo mod graph' it should resolve itself to the most recent ver
_, ok := err.(*exec.ExitError)
if !ok {
return errors.Errorf("failed to execute 'go %v': %s %T", args, err, err)
return fmt.Errorf("failed to execute 'go %v': %s %T", args, err, err)
}
// Too old Go version
@ -666,7 +667,7 @@ If you then run 'hugo mod graph' it should resolve itself to the most recent ver
return nil
}
return errors.Errorf("go command failed: %s", stderr)
return fmt.Errorf("go command failed: %s", stderr)
}
@ -706,7 +707,7 @@ func (c *Client) shouldVendor(path string) bool {
}
func (c *Client) createThemeDirname(modulePath string, isProjectMod bool) (string, error) {
invalid := errors.Errorf("invalid module path %q; must be relative to themesDir when defined outside of the project", modulePath)
invalid := fmt.Errorf("invalid module path %q; must be relative to themesDir when defined outside of the project", modulePath)
modulePath = filepath.Clean(modulePath)
if filepath.IsAbs(modulePath) {

View file

@ -36,7 +36,7 @@ import (
"github.com/rogpeppe/go-internal/module"
"github.com/pkg/errors"
"errors"
"github.com/gohugoio/hugo/config"
"github.com/spf13/afero"
@ -48,7 +48,7 @@ const vendorModulesFilename = "modules.txt"
// IsNotExist returns whether an error means that a module could not be found.
func IsNotExist(err error) bool {
return errors.Cause(err) == ErrNotExist
return errors.Is(err, os.ErrNotExist)
}
// CreateProjectModule creates modules from the given config.
@ -289,7 +289,7 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool
return nil, nil
}
if found, _ := afero.Exists(c.fs, moduleDir); !found {
c.err = c.wrapModuleNotFound(errors.Errorf(`module %q not found; either add it as a Hugo Module or store it in %q.`, modulePath, c.ccfg.ThemesDir))
c.err = c.wrapModuleNotFound(fmt.Errorf(`module %q not found; either add it as a Hugo Module or store it in %q.`, modulePath, c.ccfg.ThemesDir))
return nil, nil
}
}
@ -297,7 +297,7 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool
}
if found, _ := afero.Exists(c.fs, moduleDir); !found {
c.err = c.wrapModuleNotFound(errors.Errorf("%q not found", moduleDir))
c.err = c.wrapModuleNotFound(fmt.Errorf("%q not found", moduleDir))
return nil, nil
}
@ -557,7 +557,7 @@ func (c *collector) collectModulesTXT(owner Module) error {
line = strings.TrimSpace(line)
parts := strings.Fields(line)
if len(parts) != 2 {
return errors.Errorf("invalid modules list: %q", filename)
return fmt.Errorf("invalid modules list: %q", filename)
}
path := parts[0]
@ -662,7 +662,7 @@ func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mou
targetBase = mnt.Target[0:idxPathSep]
}
if !files.IsComponentFolder(targetBase) {
return nil, errors.Errorf("%s: mount target must be one of: %v", errMsg, files.ComponentFolders)
return nil, fmt.Errorf("%s: mount target must be one of: %v", errMsg, files.ComponentFolders)
}
out = append(out, mnt)
@ -672,7 +672,7 @@ func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mou
}
func (c *collector) wrapModuleNotFound(err error) error {
err = errors.Wrap(ErrNotExist, err.Error())
err = fmt.Errorf(err.Error()+": %w", ErrNotExist)
if c.GoModulesFilename == "" {
return err
}
@ -681,9 +681,9 @@ func (c *collector) wrapModuleNotFound(err error) error {
switch c.goBinaryStatus {
case goBinaryStatusNotFound:
return errors.Wrap(err, baseMsg+" you need to install Go to use it. See https://golang.org/dl/.")
return fmt.Errorf(baseMsg+" you need to install Go to use it. See https://golang.org/dl/ : %q", err)
case goBinaryStatusTooOld:
return errors.Wrap(err, baseMsg+" you need to a newer version of Go to use it. See https://golang.org/dl/.")
return fmt.Errorf(baseMsg+" you need to a newer version of Go to use it. See https://golang.org/dl/ : %w", err)
}
return err

View file

@ -18,8 +18,6 @@ import (
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/config"
@ -226,7 +224,7 @@ func decodeConfig(cfg config.Provider, pathReplacements map[string]string) (Conf
for _, repl := range c.Replacements {
parts := strings.Split(repl, "->")
if len(parts) != 2 {
return c, errors.Errorf(`invalid module.replacements: %q; configure replacement pairs on the form "oldpath->newpath" `, repl)
return c, fmt.Errorf(`invalid module.replacements: %q; configure replacement pairs on the form "oldpath->newpath" `, repl)
}
c.replacementsMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])

View file

@ -24,8 +24,6 @@ import (
"github.com/gohugoio/hugo/hugofs/files"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
@ -57,7 +55,7 @@ func Pack(fs afero.Fs, fis []hugofs.FileMetaInfo) error {
if err == nil {
// Preserve the original in package.hugo.json.
if err = hugio.CopyFile(fs, packageJSONName, files.FilenamePackageHugoJSON); err != nil {
return errors.Wrap(err, "npm pack: failed to copy package file")
return fmt.Errorf("npm pack: failed to copy package file: %w", err)
}
} else {
// Create one.
@ -83,7 +81,7 @@ func Pack(fs afero.Fs, fis []hugofs.FileMetaInfo) error {
masterFilename := meta.Filename
f, err := meta.Open()
if err != nil {
return errors.Wrap(err, "npm pack: failed to open package file")
return fmt.Errorf("npm pack: failed to open package file: %w", err)
}
b = newPackageBuilder(meta.Module, f)
f.Close()
@ -106,14 +104,14 @@ func Pack(fs afero.Fs, fis []hugofs.FileMetaInfo) error {
f, err := meta.Open()
if err != nil {
return errors.Wrap(err, "npm pack: failed to open package file")
return fmt.Errorf("npm pack: failed to open package file: %w", err)
}
b.Add(meta.Module, f)
f.Close()
}
if b.Err() != nil {
return errors.Wrap(b.Err(), "npm pack: failed to build")
return fmt.Errorf("npm pack: failed to build: %w", b.Err())
}
// Replace the dependencies in the original template with the merged set.
@ -136,11 +134,11 @@ func Pack(fs afero.Fs, fis []hugofs.FileMetaInfo) error {
encoder.SetEscapeHTML(false)
encoder.SetIndent("", strings.Repeat(" ", 2))
if err := encoder.Encode(b.originalPackageJSON); err != nil {
return errors.Wrap(err, "npm pack: failed to marshal JSON")
return fmt.Errorf("npm pack: failed to marshal JSON: %w", err)
}
if err := afero.WriteFile(fs, packageJSONName, packageJSONData.Bytes(), 0666); err != nil {
return errors.Wrap(err, "npm pack: failed to write package.json")
return fmt.Errorf("npm pack: failed to write package.json: %w", err)
}
return nil