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
This commit is contained in:
Bjørn Erik Pedersen
2022-05-02 16:07:52 +02:00
parent 6eea32bd6b
commit f2946da9e8
109 changed files with 861 additions and 780 deletions

View File

@@ -0,0 +1,63 @@
<!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;
color: #48b685;
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" }}
{{ range $i, $e := .Files }}
{{ if not .ErrorContext }}
{{ continue }}
{{ end }}
{{ $params := printf "noclasses=true,style=paraiso-dark,linenos=table,hl_lines=%d,linenostart=%d" (add .ErrorContext.LinesPos 1) (sub .Position.LineNumber .ErrorContext.LinesPos) }}
{{ $lexer := .ErrorContext.ChromaLexer | default "go-html-template" }}
<h3><code>{{ path.Base .Position.Filename }}:</code></h3>
{{ highlight (delimit .ErrorContext.Lines "\n") $lexer $params }}
{{ end }}
<p class="version">{{ .Version }}</p>
<a href="">Reload Page</a>
</main>
</body>
</html>

View File

@@ -17,6 +17,7 @@ import (
"bytes"
"context"
"embed"
"fmt"
"io"
"io/fs"
"os"
@@ -42,7 +43,6 @@ import (
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/pkg/errors"
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
@@ -551,14 +551,10 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error
}
defer f.Close()
fe, ok := herrors.WithFileContext(inErr, info.realFilename, f, lineMatcher)
if ok {
return fe, true
}
return inErr, false
return herrors.NewFileError(info.realFilename, inErr).UpdateContent(f, lineMatcher), true
}
inerr = errors.Wrap(inerr, "execute of template failed")
inerr = fmt.Errorf("execute of template failed: %w", inerr)
if err, ok := checkFilename(ts.info, inerr); ok {
return err
@@ -736,6 +732,7 @@ func (t *templateHandler) extractIdentifiers(line string) []string {
//go:embed embedded/templates/*
//go:embed embedded/templates/_default/*
//go:embed embedded/templates/server/*
var embededTemplatesFs embed.FS
func (t *templateHandler) loadEmbedded() error {
@@ -755,10 +752,10 @@ func (t *templateHandler) loadEmbedded() error {
name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/")
templateName := name
// For the render hooks it does not make sense to preseve the
// For the render hooks and the server templates it does not make sense to preseve the
// double _indternal double book-keeping,
// just add it if its now provided by the user.
if !strings.Contains(path, "_default/_markup") {
if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "server/") {
templateName = internalPathPrefix + name
}

View File

@@ -14,6 +14,7 @@
package tplimpl
import (
"fmt"
"regexp"
"strings"
@@ -22,10 +23,11 @@ import (
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
"errors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/tpl"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
type templateType int
@@ -239,14 +241,14 @@ func (c *templateContext) collectConfig(n *parse.PipeNode) {
}
if s, ok := cmd.Args[0].(*parse.StringNode); ok {
errMsg := "failed to decode $_hugo_config in template"
errMsg := "failed to decode $_hugo_config in template: %w"
m, err := maps.ToStringMapE(s.Text)
if err != nil {
c.err = errors.Wrap(err, errMsg)
c.err = fmt.Errorf(errMsg, err)
return
}
if err := mapstructure.WeakDecode(m, &c.t.parseInfo.Config); err != nil {
c.err = errors.Wrap(err, errMsg)
c.err = fmt.Errorf(errMsg, err)
}
}
}

View File

@@ -14,8 +14,9 @@
package tplimpl
import (
"fmt"
"github.com/gohugoio/hugo/common/herrors"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
@@ -51,14 +52,13 @@ func (t templateInfo) resolveType() templateType {
}
func (info templateInfo) errWithFileContext(what string, err error) error {
err = errors.Wrapf(err, what)
err = fmt.Errorf(what+": %w", err)
fe := herrors.NewFileError(info.realFilename, err)
f, err := info.fs.Open(info.filename)
if err != nil {
return err
}
defer f.Close()
return fe.UpdateContent(f, herrors.SimpleLineMatcher)
err, _ = herrors.WithFileContextForFile(
err,
info.realFilename,
info.filename,
info.fs,
herrors.SimpleLineMatcher)
return err
}