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) {