diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-02 16:07:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-06 19:43:22 +0200 |
commit | f2946da9e806c2bafbdd26707fe339db79bd980b (patch) | |
tree | b5609317a861ea5f399e094e1b9287ca71dc22d1 /resources/resource_transformers/js | |
parent | 6eea32bd6bc8e7a7dd07a8cb6a8343ae2c74aba0 (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/js')
-rw-r--r-- | resources/resource_transformers/js/build.go | 15 | ||||
-rw-r--r-- | resources/resource_transformers/js/options.go | 5 |
2 files changed, 11 insertions, 9 deletions
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{ |