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/server.go | |
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/server.go')
-rw-r--r-- | commands/server.go | 30 |
1 files changed, 21 insertions, 9 deletions
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) |