mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
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:
@@ -15,6 +15,7 @@ package babel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -34,7 +35,6 @@ import (
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Options from https://babeljs.io/docs/en/options
|
||||
@@ -141,7 +141,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
|
||||
if configFile == "" && t.options.Config != "" {
|
||||
// Only fail if the user specified config file is not found.
|
||||
return errors.Errorf("babel config %q not found:", configFile)
|
||||
return fmt.Errorf("babel config %q not found:", configFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
if hexec.IsNotFound(err) {
|
||||
return herrors.ErrFeatureNotAvailable
|
||||
}
|
||||
return errors.Wrap(err, errBuf.String())
|
||||
return fmt.Errorf(errBuf.String()+": %w", err)
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadAll(compileOutput)
|
||||
|
@@ -19,14 +19,13 @@ import (
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"html/template"
|
||||
"io"
|
||||
|
||||
"github.com/gohugoio/hugo/resources/internal"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
)
|
||||
@@ -92,7 +91,7 @@ func newHash(algo string) (hash.Hash, error) {
|
||||
case "sha512":
|
||||
return sha512.New(), nil
|
||||
default:
|
||||
return nil, errors.Errorf("unsupported crypto algo: %q, use either md5, sha256, sha384 or sha512", algo)
|
||||
return nil, fmt.Errorf("unsupported crypto algo: %q, use either md5, sha256, sha384 or sha512", algo)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -22,13 +22,14 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"errors"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
"github.com/gohugoio/hugo/common/text"
|
||||
|
||||
"github.com/gohugoio/hugo/hugolib/filesystems"
|
||||
"github.com/gohugoio/hugo/media"
|
||||
@@ -109,13 +110,13 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
for i, ext := range opts.Inject {
|
||||
impPath := filepath.FromSlash(ext)
|
||||
if filepath.IsAbs(impPath) {
|
||||
return errors.Errorf("inject: absolute paths not supported, must be relative to /assets")
|
||||
return fmt.Errorf("inject: absolute paths not supported, must be relative to /assets")
|
||||
}
|
||||
|
||||
m := resolveComponentInAssets(t.c.rs.Assets.Fs, impPath)
|
||||
|
||||
if m == nil {
|
||||
return errors.Errorf("inject: file %q not found", ext)
|
||||
return fmt.Errorf("inject: file %q not found", ext)
|
||||
}
|
||||
|
||||
opts.Inject[i] = m.Filename
|
||||
@@ -157,10 +158,12 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
fe := herrors.NewFileError("js", 0, loc.Line, loc.Column, errors.New(msg.Text))
|
||||
err, _ := herrors.WithFileContext(fe, path, f, herrors.SimpleLineMatcher)
|
||||
fe := herrors.NewFileError(path, errors.New(msg.Text)).
|
||||
UpdatePosition(text.Position{Offset: -1, LineNumber: loc.Line, ColumnNumber: loc.Column}).
|
||||
UpdateContent(f, herrors.SimpleLineMatcher)
|
||||
|
||||
f.Close()
|
||||
return err
|
||||
return fe
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s", msg.Text)
|
||||
|
@@ -21,7 +21,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/evanw/esbuild/pkg/api"
|
||||
@@ -251,7 +250,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
||||
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
|
||||
b, err := ioutil.ReadFile(args.Path)
|
||||
if err != nil {
|
||||
return api.OnLoadResult{}, errors.Wrapf(err, "failed to read %q", args.Path)
|
||||
return api.OnLoadResult{}, fmt.Errorf("failed to read %q: %w", args.Path, err)
|
||||
}
|
||||
c := string(b)
|
||||
return api.OnLoadResult{
|
||||
@@ -274,7 +273,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
||||
|
||||
b, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to marshal params")
|
||||
return nil, fmt.Errorf("failed to marshal params: %w", err)
|
||||
}
|
||||
bs := string(b)
|
||||
paramsPlugin := api.Plugin{
|
||||
|
@@ -17,6 +17,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
@@ -36,8 +37,9 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/cast"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
|
||||
@@ -161,7 +163,7 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
|
||||
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
|
||||
if configFile == "" && t.options.Config != "" {
|
||||
// Only fail if the user specified config file is not found.
|
||||
return errors.Errorf("postcss config %q not found:", configFile)
|
||||
return fmt.Errorf("postcss config %q not found:", configFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,15 +390,9 @@ func (imp *importResolver) toFileError(output string) error {
|
||||
if err != nil {
|
||||
return inErr
|
||||
}
|
||||
|
||||
realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename
|
||||
|
||||
ferr := herrors.NewFileError("css", -1, file.Offset+1, 1, inErr)
|
||||
return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher)
|
||||
|
||||
werr, ok := herrors.WithFileContextForFile(ferr, realFilename, file.Filename, imp.fs, herrors.SimpleLineMatcher)
|
||||
|
||||
if !ok {
|
||||
return ferr
|
||||
}
|
||||
|
||||
return werr
|
||||
}
|
||||
|
@@ -15,12 +15,13 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
"github.com/gohugoio/hugo/resources/internal"
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
"github.com/gohugoio/hugo/tpl"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Client contains methods to perform template processing of Resource objects.
|
||||
@@ -55,7 +56,7 @@ func (t *executeAsTemplateTransform) Transform(ctx *resources.ResourceTransforma
|
||||
tplStr := helpers.ReaderToString(ctx.From)
|
||||
templ, err := t.t.TextTmpl().Parse(ctx.InPath, tplStr)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to parse Resource %q as Template:", ctx.InPath)
|
||||
return fmt.Errorf("failed to parse Resource %q as Template:: %w", ctx.InPath, err)
|
||||
}
|
||||
|
||||
ctx.OutPath = t.targetPath
|
||||
|
@@ -120,18 +120,8 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
|
||||
return m.Offset+len(m.Line) >= start.Offset && strings.Contains(m.Line, context)
|
||||
}
|
||||
|
||||
ferr, ok := herrors.WithFileContextForFile(
|
||||
herrors.NewFileError("scss", -1, -1, start.Column, sassErr),
|
||||
filename,
|
||||
filename,
|
||||
hugofs.Os,
|
||||
offsetMatcher)
|
||||
return herrors.NewFileErrorFromFile(sassErr, filename, filename, hugofs.Os, offsetMatcher)
|
||||
|
||||
if !ok {
|
||||
return sassErr
|
||||
}
|
||||
|
||||
return ferr
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
"github.com/gohugoio/hugo/media"
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Used in tests. This feature requires Hugo to be built with the extended tag.
|
||||
@@ -172,7 +171,7 @@ func (c *Client) toCSS(options libsass.Options, dst io.Writer, src io.Reader) (l
|
||||
in := helpers.ReaderToString(src)
|
||||
|
||||
// See https://github.com/gohugoio/hugo/issues/7059
|
||||
// We need to preserver the regular CSS imports. This is by far
|
||||
// We need to preserve the regular CSS imports. This is by far
|
||||
// a perfect solution, and only works for the main entry file, but
|
||||
// that should cover many use cases, e.g. using SCSS as a preprocessor
|
||||
// for Tailwind.
|
||||
@@ -181,7 +180,7 @@ func (c *Client) toCSS(options libsass.Options, dst io.Writer, src io.Reader) (l
|
||||
|
||||
res, err = transpiler.Execute(in)
|
||||
if err != nil {
|
||||
return res, errors.Wrap(err, "SCSS processing failed")
|
||||
return res, fmt.Errorf("SCSS processing failed: %w", err)
|
||||
}
|
||||
|
||||
out := res.CSS
|
||||
|
Reference in New Issue
Block a user