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

@@ -27,7 +27,7 @@ import (
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/paths"
"github.com/pkg/errors"
"errors"
"github.com/gohugoio/hugo/hugofs/files"
@@ -94,11 +94,11 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string) error {
}
if ext == "" {
return "", errors.Errorf("failed to resolve %q to a archetype template", targetPath)
return "", fmt.Errorf("failed to resolve %q to a archetype template", targetPath)
}
if !files.IsContentFile(b.targetPath) {
return "", errors.Errorf("target path %q is not a known content format", b.targetPath)
return "", fmt.Errorf("target path %q is not a known content format", b.targetPath)
}
return b.buildFile()
@@ -188,14 +188,14 @@ func (b *contentBuilder) buildDir() error {
in, err := meta.Open()
if err != nil {
return errors.Wrap(err, "failed to open non-content file")
return fmt.Errorf("failed to open non-content file: %w", err)
}
targetFilename := filepath.Join(baseDir, b.targetPath, strings.TrimPrefix(filename, b.archetypeFilename))
targetDir := filepath.Dir(targetFilename)
if err := b.sourceFs.MkdirAll(targetDir, 0o777); err != nil && !os.IsExist(err) {
return errors.Wrapf(err, "failed to create target directory for %q", targetDir)
return fmt.Errorf("failed to create target directory for %q: %w", targetDir, err)
}
out, err := b.sourceFs.Create(targetFilename)
@@ -329,7 +329,7 @@ func (b *contentBuilder) mapArcheTypeDir() error {
w := hugofs.NewWalkway(walkCfg)
if err := w.Walk(); err != nil {
return errors.Wrapf(err, "failed to walk archetype dir %q", b.archetypeFilename)
return fmt.Errorf("failed to walk archetype dir %q: %w", b.archetypeFilename, err)
}
b.dirMap = m
@@ -374,7 +374,7 @@ func (b *contentBuilder) usesSiteVar(filename string) (bool, error) {
}
bb, err := afero.ReadFile(b.archeTypeFs, filename)
if err != nil {
return false, errors.Wrap(err, "failed to open archetype file")
return false, fmt.Errorf("failed to open archetype file: %w", err)
}
return bytes.Contains(bb, []byte(".Site")) || bytes.Contains(bb, []byte("site.")), nil