summaryrefslogtreecommitdiffstats
path: root/resources/resource_transformers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-02 16:07:52 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-06 19:43:22 +0200
commitf2946da9e806c2bafbdd26707fe339db79bd980b (patch)
treeb5609317a861ea5f399e094e1b9287ca71dc22d1 /resources/resource_transformers
parent6eea32bd6bc8e7a7dd07a8cb6a8343ae2c74aba0 (diff)
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
Diffstat (limited to 'resources/resource_transformers')
-rw-r--r--resources/resource_transformers/babel/babel.go6
-rw-r--r--resources/resource_transformers/integrity/integrity.go5
-rw-r--r--resources/resource_transformers/js/build.go15
-rw-r--r--resources/resource_transformers/js/options.go5
-rw-r--r--resources/resource_transformers/postcss/postcss.go16
-rw-r--r--resources/resource_transformers/templates/execute_as_template.go5
-rw-r--r--resources/resource_transformers/tocss/dartsass/transform.go12
-rw-r--r--resources/resource_transformers/tocss/scss/tocss.go5
8 files changed, 28 insertions, 41 deletions
diff --git a/resources/resource_transformers/babel/babel.go b/resources/resource_transformers/babel/babel.go
index a4744961e..9a9110f62 100644
--- a/resources/resource_transformers/babel/babel.go
+++ b/resources/resource_transformers/babel/babel.go
@@ -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)
diff --git a/resources/resource_transformers/integrity/integrity.go b/resources/resource_transformers/integrity/integrity.go
index bbd0b6675..e15754685 100644
--- a/resources/resource_transformers/integrity/integrity.go
+++ b/resources/resource_transformers/integrity/integrity.go
@@ -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)
}
}
diff --git a/resources/resource_transformers/js/build.go b/resources/resource_transformers/js/build.go
index 3c6f24fc0..d2fbf5065 100644
--- a/resources/resource_transformers/js/build.go
+++ b/resources/resource_transformers/js/build.go
@@ -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)
diff --git a/resources/resource_transformers/js/options.go b/resources/resource_transformers/js/options.go
index 675e40d43..a0d1b506f 100644
--- a/resources/resource_transformers/js/options.go
+++ b/resources/resource_transformers/js/options.go
@@ -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{
diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go
index 61209fb23..733c958cf 100644
--- a/resources/resource_transformers/postcss/postcss.go
+++ b/resources/resource_transformers/postcss/postcss.go
@@ -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)
- werr, ok := herrors.WithFileContextForFile(ferr, realFilename, file.Filename, imp.fs, herrors.SimpleLineMatcher)
+ realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename
- if !ok {
- return ferr
- }
+ return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher)
- return werr
}
diff --git a/resources/resource_transformers/templates/execute_as_template.go b/resources/resource_transformers/templates/execute_as_template.go
index 5c2033ff9..5fe4230f1 100644
--- a/resources/resource_transformers/templates/execute_as_template.go
+++ b/resources/resource_transformers/templates/execute_as_template.go
@@ -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
diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go
index 79c32fcfd..082e30710 100644
--- a/resources/resource_transformers/tocss/dartsass/transform.go
+++ b/resources/resource_transformers/tocss/dartsass/transform.go
@@ -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)
-
- if !ok {
- return sassErr
- }
+ return herrors.NewFileErrorFromFile(sassErr, filename, filename, hugofs.Os, offsetMatcher)
- return ferr
}
return err
}
diff --git a/resources/resource_transformers/tocss/scss/tocss.go b/resources/resource_transformers/tocss/scss/tocss.go
index 802798e59..f9f4786f5 100644
--- a/resources/resource_transformers/tocss/scss/tocss.go
+++ b/resources/resource_transformers/tocss/scss/tocss.go
@@ -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