From f2946da9e806c2bafbdd26707fe339db79bd980b Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Mon, 2 May 2022 16:07:52 +0200 Subject: 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 --- transform/chain.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'transform') diff --git a/transform/chain.go b/transform/chain.go index 74217dc72..2d70b9356 100644 --- a/transform/chain.go +++ b/transform/chain.go @@ -16,8 +16,11 @@ package transform import ( "bytes" "io" + "io/ioutil" bp "github.com/gohugoio/hugo/bufferpool" + "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/hugofs" ) // Transformer is the func that needs to be implemented by a transformation step. @@ -103,7 +106,17 @@ func (c *Chain) Apply(to io.Writer, from io.Reader) error { } if err := tr(fb); err != nil { - return err + // Write output to a temp file so it can be read by the user for trouble shooting. + filename := "output.html" + tempfile, ferr := ioutil.TempFile("", "hugo-transform-error") + if ferr == nil { + filename = tempfile.Name() + defer tempfile.Close() + _, _ = io.Copy(tempfile, fb.from) + return herrors.NewFileErrorFromFile(err, filename, filename, hugofs.Os, nil) + } + return herrors.NewFileError(filename, err).UpdateContent(fb.from, nil) + } } -- cgit v1.2.3