summaryrefslogtreecommitdiffstats
path: root/tpl/tplimpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-02 16:07:52 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-06 19:43:22 +0200
commitf2946da9e806c2bafbdd26707fe339db79bd980b (patch)
treeb5609317a861ea5f399e094e1b9287ca71dc22d1 /tpl/tplimpl
parent6eea32bd6bc8e7a7dd07a8cb6a8343ae2c74aba0 (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 'tpl/tplimpl')
-rw-r--r--tpl/tplimpl/embedded/templates/server/error.html63
-rw-r--r--tpl/tplimpl/template.go15
-rw-r--r--tpl/tplimpl/template_ast_transformers.go10
-rw-r--r--tpl/tplimpl/template_errors.go20
4 files changed, 85 insertions, 23 deletions
diff --git a/tpl/tplimpl/embedded/templates/server/error.html b/tpl/tplimpl/embedded/templates/server/error.html
new file mode 100644
index 000000000..e5c6d0cbf
--- /dev/null
+++ b/tpl/tplimpl/embedded/templates/server/error.html
@@ -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>
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go
index 4cee19a20..c092ff638 100644
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -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
}
diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go
index 33461dc7d..dee1fd6c2 100644
--- a/tpl/tplimpl/template_ast_transformers.go
+++ b/tpl/tplimpl/template_ast_transformers.go
@@ -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)
}
}
}
diff --git a/tpl/tplimpl/template_errors.go b/tpl/tplimpl/template_errors.go
index 06d895536..d553a12e9 100644
--- a/tpl/tplimpl/template_errors.go
+++ b/tpl/tplimpl/template_errors.go
@@ -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, _ = herrors.WithFileContextForFile(
- err,
- info.realFilename,
- info.filename,
- info.fs,
- herrors.SimpleLineMatcher)
+ 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)
- return err
}