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 /commands | |
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 'commands')
-rw-r--r-- | commands/commandeer.go | 17 | ||||
-rw-r--r-- | commands/convert.go | 4 | ||||
-rw-r--r-- | commands/hugo.go | 19 | ||||
-rw-r--r-- | commands/new_site.go | 5 | ||||
-rw-r--r-- | commands/server.go | 30 | ||||
-rw-r--r-- | commands/server_errors.go | 61 |
6 files changed, 35 insertions, 101 deletions
diff --git a/commands/commandeer.go b/commands/commandeer.go index c42de5d11..5b192c172 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -14,7 +14,6 @@ package commands import ( - "bytes" "errors" "io/ioutil" "net" @@ -141,19 +140,11 @@ func (c *commandeer) getErrorWithContext() any { m := make(map[string]any) - m["Error"] = errors.New(removeErrorPrefixFromLog(c.logger.Errors())) + //xwm["Error"] = errors.New(cleanErrorLog(removeErrorPrefixFromLog(c.logger.Errors()))) + m["Error"] = errors.New(cleanErrorLog(removeErrorPrefixFromLog(c.logger.Errors()))) m["Version"] = hugo.BuildVersionString() - - fe := herrors.UnwrapErrorWithFileContext(c.buildErr) - if fe != nil { - m["File"] = fe - } - - if c.h.verbose { - var b bytes.Buffer - herrors.FprintStackTraceFromErr(&b, c.buildErr) - m["StackTrace"] = b.String() - } + ferrors := herrors.UnwrapFileErrorsWithErrorContext(c.buildErr) + m["Files"] = ferrors return m } diff --git a/commands/convert.go b/commands/convert.go index d3cad2bd6..1ec965a0b 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -31,8 +31,6 @@ import ( "github.com/gohugoio/hugo/parser" "github.com/gohugoio/hugo/parser/metadecoders" - "github.com/pkg/errors" - "github.com/gohugoio/hugo/hugolib" "github.com/spf13/cobra" @@ -193,7 +191,7 @@ func (cc *convertCmd) convertAndSavePage(p page.Page, site *hugolib.Site, target fs := hugofs.Os if err := helpers.WriteToDisk(newFilename, &newContent, fs); err != nil { - return errors.Wrapf(err, "Failed to save file %q:", newFilename) + return fmt.Errorf("Failed to save file %q:: %w", newFilename, err) } return nil diff --git a/commands/hugo.go b/commands/hugo.go index 9033fac90..6bd0605db 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -39,9 +39,6 @@ import ( "github.com/gohugoio/hugo/resources/page" - "github.com/pkg/errors" - - "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/terminal" @@ -300,14 +297,14 @@ func (c *commandeer) fullBuild(noBuildLock bool) error { copyStaticFunc := func() error { cnt, err := c.copyStatic() if err != nil { - return errors.Wrap(err, "Error copying static files") + return fmt.Errorf("Error copying static files: %w", err) } langCount = cnt return nil } buildSitesFunc := func() error { if err := c.buildSites(noBuildLock); err != nil { - return errors.Wrap(err, "Error building site") + return fmt.Errorf("Error building site: %w", err) } return nil } @@ -354,10 +351,10 @@ func (c *commandeer) initCPUProfile() (func(), error) { f, err := os.Create(c.h.cpuprofile) if err != nil { - return nil, errors.Wrap(err, "failed to create CPU profile") + return nil, fmt.Errorf("failed to create CPU profile: %w", err) } if err := pprof.StartCPUProfile(f); err != nil { - return nil, errors.Wrap(err, "failed to start CPU profile") + return nil, fmt.Errorf("failed to start CPU profile: %w", err) } return func() { pprof.StopCPUProfile() @@ -388,11 +385,11 @@ func (c *commandeer) initTraceProfile() (func(), error) { f, err := os.Create(c.h.traceprofile) if err != nil { - return nil, errors.Wrap(err, "failed to create trace file") + return nil, fmt.Errorf("failed to create trace file: %w", err) } if err := trace.Start(f); err != nil { - return nil, errors.Wrap(err, "failed to start trace") + return nil, fmt.Errorf("failed to start trace: %w", err) } return func() { @@ -735,9 +732,7 @@ func (c *commandeer) handleBuildErr(err error, msg string) { c.logger.Errorln(msg + ":\n") c.logger.Errorln(helpers.FirstUpper(err.Error())) - if !c.h.quiet && c.h.verbose { - herrors.PrintStackTraceFromErr(err) - } + } func (c *commandeer) rebuildSites(events []fsnotify.Event) error { diff --git a/commands/new_site.go b/commands/new_site.go index e49a60202..384c6365b 100644 --- a/commands/new_site.go +++ b/commands/new_site.go @@ -16,14 +16,13 @@ package commands import ( "bytes" "errors" + "fmt" "path/filepath" "strings" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/parser/metadecoders" - _errors "github.com/pkg/errors" - "github.com/gohugoio/hugo/create" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugofs" @@ -94,7 +93,7 @@ func (n *newSiteCmd) doNewSite(fs *hugofs.Fs, basepath string, force bool) error for _, dir := range dirs { if err := fs.Source.MkdirAll(dir, 0777); err != nil { - return _errors.Wrap(err, "Failed to create dir") + return fmt.Errorf("Failed to create dir: %w", err) } } diff --git a/commands/server.go b/commands/server.go index 145a889bb..985775336 100644 --- a/commands/server.go +++ b/commands/server.go @@ -36,8 +36,6 @@ import ( "github.com/gohugoio/hugo/common/paths" "golang.org/x/sync/errgroup" - "github.com/pkg/errors" - "github.com/gohugoio/hugo/livereload" "github.com/gohugoio/hugo/config" @@ -366,7 +364,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string // We're only interested in the path u, err := url.Parse(baseURL) if err != nil { - return nil, nil, "", "", errors.Wrap(err, "Invalid baseURL") + return nil, nil, "", "", fmt.Errorf("Invalid baseURL: %w", err) } decorate := func(h http.Handler) http.Handler { @@ -480,6 +478,21 @@ var logErrorRe = regexp.MustCompile(`(?s)ERROR \d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{ func removeErrorPrefixFromLog(content string) string { return logErrorRe.ReplaceAllLiteralString(content, "") } +func cleanErrorLog(content string) string { + content = strings.ReplaceAll(content, "Rebuild failed:\n", "") + content = strings.ReplaceAll(content, "\n", "") + seen := make(map[string]bool) + parts := strings.Split(content, ": ") + keep := make([]string, 0, len(parts)) + for _, part := range parts { + if seen[part] { + continue + } + seen[part] = true + keep = append(keep, part) + } + return strings.Join(keep, ": ") +} func (c *commandeer) serve(s *serverCmd) error { isMultiHost := c.hugo().IsMultihost() @@ -500,17 +513,16 @@ func (c *commandeer) serve(s *serverCmd) error { roots = []string{""} } - templ, err := c.hugo().TextTmpl().Parse("__default_server_error", buildErrorTemplate) - if err != nil { - return err - } - srv := &fileServer{ baseURLs: baseURLs, roots: roots, c: c, s: s, errorTemplate: func(ctx any) (io.Reader, error) { + templ, found := c.hugo().Tmpl().Lookup("server/error.html") + if !found { + panic("template server/error.html not found") + } b := &bytes.Buffer{} err := c.hugo().Tmpl().Execute(templ, b, ctx) return b, err @@ -627,7 +639,7 @@ func (sc *serverCmd) fixURL(cfg config.Provider, s string, port int) (string, er if strings.Contains(u.Host, ":") { u.Host, _, err = net.SplitHostPort(u.Host) if err != nil { - return "", errors.Wrap(err, "Failed to split baseURL hostpost") + return "", fmt.Errorf("Failed to split baseURL hostpost: %w", err) } } u.Host += fmt.Sprintf(":%d", port) diff --git a/commands/server_errors.go b/commands/server_errors.go index 0e3bc50de..edf658156 100644 --- a/commands/server_errors.go +++ b/commands/server_errors.go @@ -22,67 +22,6 @@ import ( "github.com/gohugoio/hugo/transform/livereloadinject" ) -var buildErrorTemplate = `<!doctype html> -<html class="no-js" lang=""> - <head> - <meta charset="utf-8"> - <title>Hugo Server: Error</title> - <style type="text/css"> - body { - font-family: "Muli",avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; - font-size: 16px; - background-color: #2f1e2e; - } - main { - margin: auto; - width: 95%; - padding: 1rem; - } - .version { - color: #ccc; - padding: 1rem 0; - } - .stack { - margin-top: 4rem; - } - pre { - white-space: pre-wrap; - white-space: -moz-pre-wrap; - white-space: -pre-wrap; - white-space: -o-pre-wrap; - word-wrap: break-word; - } - .highlight { - overflow-x: auto; - margin-bottom: 1rem; - } - a { - color: #0594cb; - text-decoration: none; - } - a:hover { - color: #ccc; - } - </style> - </head> - <body> - <main> - {{ highlight .Error "apl" "linenos=false,noclasses=true,style=paraiso-dark" }} - {{ with .File }} - {{ $params := printf "noclasses=true,style=paraiso-dark,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }} - {{ $lexer := .ChromaLexer | default "go-html-template" }} - {{ highlight (delimit .Lines "\n") $lexer $params }} - {{ end }} - {{ with .StackTrace }} - {{ highlight . "apl" "noclasses=true,style=paraiso-dark" }} - {{ end }} - <p class="version">{{ .Version }}</p> - <a href="">Reload Page</a> - </main> -</body> -</html> -` - func injectLiveReloadScript(src io.Reader, baseURL url.URL) string { var b bytes.Buffer chain := transform.Chain{livereloadinject.New(baseURL)} |