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

@@ -24,12 +24,13 @@ import (
"strings"
"time"
"errors"
"github.com/gohugoio/hugo/common/collections"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
@@ -736,7 +737,7 @@ func (ns *Namespace) Uniq(seq any) (any, error) {
case reflect.Array:
slice = reflect.MakeSlice(reflect.SliceOf(v.Type().Elem()), 0, 0)
default:
return nil, errors.Errorf("type %T not supported", seq)
return nil, fmt.Errorf("type %T not supported", seq)
}
seen := make(map[any]bool)

View File

@@ -14,13 +14,14 @@
package collections
import (
"fmt"
"reflect"
"strings"
"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/common/maps"
"github.com/pkg/errors"
"errors"
)
// Merge creates a copy of the final parameter and merges the preceding
@@ -49,7 +50,7 @@ func (ns *Namespace) merge(src, dst any) (any, error) {
vdst, vsrc := reflect.ValueOf(dst), reflect.ValueOf(src)
if vdst.Kind() != reflect.Map {
return nil, errors.Errorf("destination must be a map, got %T", dst)
return nil, fmt.Errorf("destination must be a map, got %T", dst)
}
if !hreflect.IsTruthfulValue(vsrc) {
@@ -57,11 +58,11 @@ func (ns *Namespace) merge(src, dst any) (any, error) {
}
if vsrc.Kind() != reflect.Map {
return nil, errors.Errorf("source must be a map, got %T", src)
return nil, fmt.Errorf("source must be a map, got %T", src)
}
if vsrc.Type().Key() != vdst.Type().Key() {
return nil, errors.Errorf("incompatible map types, got %T to %T", src, dst)
return nil, fmt.Errorf("incompatible map types, got %T to %T", src, dst)
}
return mergeMap(vdst, vsrc).Interface(), nil

View File

@@ -18,8 +18,9 @@ import (
"reflect"
"time"
"errors"
"github.com/mitchellh/hashstructure"
"github.com/pkg/errors"
)
var (
@@ -103,7 +104,7 @@ func convertValue(v reflect.Value, to reflect.Type) (reflect.Value, error) {
case isNumber(kind):
return convertNumber(v, kind)
default:
return reflect.Value{}, errors.Errorf("%s is not assignable to %s", v.Type(), to)
return reflect.Value{}, fmt.Errorf("%s is not assignable to %s", v.Type(), to)
}
}

View File

@@ -16,8 +16,6 @@ package collections
import (
"fmt"
"reflect"
"github.com/pkg/errors"
)
// SymDiff returns the symmetric difference of s1 and s2.
@@ -54,7 +52,7 @@ func (ns *Namespace) SymDiff(s2, s1 any) (any, error) {
if ids1[key] != ids2[key] {
v, err := convertValue(ev, sliceElemType)
if err != nil {
return nil, errors.WithMessage(err, "symdiff: failed to convert value")
return nil, fmt.Errorf("symdiff: failed to convert value: %w", err)
}
slice = reflect.Append(slice, v)
}

View File

@@ -20,6 +20,7 @@ import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
@@ -35,7 +36,6 @@ import (
"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/deps"
_errors "github.com/pkg/errors"
)
// New returns a new instance of the data-namespaced template functions.
@@ -69,7 +69,7 @@ func (ns *Namespace) GetCSV(sep string, args ...any) (d [][]string, err error) {
unmarshal := func(b []byte) (bool, error) {
if d, err = parseCSV(b, sep); err != nil {
err = _errors.Wrapf(err, "failed to parse CSV file %s", url)
err = fmt.Errorf("failed to parse CSV file %s: %w", url, err)
return true, err
}
@@ -80,7 +80,7 @@ func (ns *Namespace) GetCSV(sep string, args ...any) (d [][]string, err error) {
var req *http.Request
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return nil, _errors.Wrapf(err, "failed to create request for getCSV for resource %s", url)
return nil, fmt.Errorf("failed to create request for getCSV for resource %s: %w", url, err)
}
// Add custom user headers.
@@ -109,7 +109,7 @@ func (ns *Namespace) GetJSON(args ...any) (any, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url)
return nil, fmt.Errorf("Failed to create request for getJSON resource %s: %w", url, err)
}
unmarshal := func(b []byte) (bool, error) {

View File

@@ -15,14 +15,13 @@ package data
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/config"
@@ -70,7 +69,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (b
res.Body.Close()
if isHTTPError(res) {
return nil, errors.Errorf("Failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
return nil, fmt.Errorf("Failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
}
retry, err = unmarshal(b)

View File

@@ -18,7 +18,7 @@ import (
"image"
"sync"
"github.com/pkg/errors"
"errors"
"github.com/gohugoio/hugo/resources/images"

View File

@@ -19,8 +19,6 @@ import (
"errors"
"fmt"
_errors "github.com/pkg/errors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/resources"
)
@@ -64,7 +62,7 @@ func ResolveArgs(args []any) (resources.ResourceTransformer, map[string]any, err
m, err := maps.ToStringMapE(args[0])
if err != nil {
return nil, nil, _errors.Wrap(err, "invalid options type")
return nil, nil, fmt.Errorf("invalid options type: %w", err)
}
return r, m, nil

View File

@@ -20,9 +20,10 @@ import (
"strconv"
"strings"
"errors"
"github.com/gohugoio/locales"
translators "github.com/gohugoio/localescompressed"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/common/hreflect"
"github.com/gohugoio/hugo/deps"
@@ -49,7 +50,7 @@ func (ns *Namespace) Translate(id any, args ...any) (string, error) {
if len(args) > 0 {
if len(args) > 1 {
return "", errors.Errorf("wrong number of arguments, expecting at most 2, got %d", len(args)+1)
return "", fmt.Errorf("wrong number of arguments, expecting at most 2, got %d", len(args)+1)
}
templateData = args[0]
}

View File

@@ -14,11 +14,12 @@
package openapi3
import (
"fmt"
"io/ioutil"
gyaml "github.com/ghodss/yaml"
"github.com/pkg/errors"
"errors"
kopenapi3 "github.com/getkin/kin-openapi/openapi3"
"github.com/gohugoio/hugo/cache/namedmemcache"
@@ -57,7 +58,7 @@ func (ns *Namespace) Unmarshal(r resource.UnmarshableResource) (*kopenapi3.T, er
v, err := ns.cache.GetOrCreate(key, func() (any, error) {
f := metadecoders.FormatFromMediaType(r.MediaType())
if f == "" {
return nil, errors.Errorf("MIME %q not supported", r.MediaType())
return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
}
reader, err := r.ReadSeekCloser()

View File

@@ -20,8 +20,9 @@ import (
"github.com/gohugoio/hugo/common/herrors"
"errors"
"github.com/gohugoio/hugo/common/maps"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
@@ -162,7 +163,7 @@ func (ns *Namespace) GetRemote(args ...any) resource.Resource {
case *create.HTTPError:
return resources.NewErrorResource(resource.NewResourceError(v, v.Data))
default:
return resources.NewErrorResource(resource.NewResourceError(errors.Wrap(err, "error calling resources.GetRemote"), make(map[string]any)))
return resources.NewErrorResource(resource.NewResourceError(fmt.Errorf("error calling resources.GetRemote: %w", err), make(map[string]any)))
}
}
@@ -354,7 +355,7 @@ func (ns *Namespace) ToCSS(args ...any) (resource.Resource, error) {
case transpilerDart, transpilerLibSass:
transpiler = cast.ToString(t)
default:
return nil, errors.Errorf("unsupported transpiler %q; valid values are %q or %q", t, transpilerLibSass, transpilerDart)
return nil, fmt.Errorf("unsupported transpiler %q; valid values are %q or %q", t, transpilerLibSass, transpilerDart)
}
}
}

View File

@@ -16,6 +16,7 @@ package strings
import (
"errors"
"fmt"
"html/template"
"regexp"
"strings"
@@ -25,7 +26,6 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
_errors "github.com/pkg/errors"
"github.com/spf13/cast"
)
@@ -48,7 +48,7 @@ type Namespace struct {
func (ns *Namespace) CountRunes(s any) (int, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return 0, _errors.Wrap(err, "Failed to convert content to string")
return 0, fmt.Errorf("Failed to convert content to string: %w", err)
}
counter := 0
@@ -65,7 +65,7 @@ func (ns *Namespace) CountRunes(s any) (int, error) {
func (ns *Namespace) RuneCount(s any) (int, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return 0, _errors.Wrap(err, "Failed to convert content to string")
return 0, fmt.Errorf("Failed to convert content to string: %w", err)
}
return utf8.RuneCountInString(ss), nil
}
@@ -74,12 +74,12 @@ func (ns *Namespace) RuneCount(s any) (int, error) {
func (ns *Namespace) CountWords(s any) (int, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return 0, _errors.Wrap(err, "Failed to convert content to string")
return 0, fmt.Errorf("Failed to convert content to string: %w", err)
}
isCJKLanguage, err := regexp.MatchString(`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`, ss)
if err != nil {
return 0, _errors.Wrap(err, "Failed to match regex pattern against string")
return 0, fmt.Errorf("Failed to match regex pattern against string: %w", err)
}
if !isCJKLanguage {
@@ -104,11 +104,11 @@ func (ns *Namespace) CountWords(s any) (int, error) {
func (ns *Namespace) Count(substr, s any) (int, error) {
substrs, err := cast.ToStringE(substr)
if err != nil {
return 0, _errors.Wrap(err, "Failed to convert substr to string")
return 0, fmt.Errorf("Failed to convert substr to string: %w", err)
}
ss, err := cast.ToStringE(s)
if err != nil {
return 0, _errors.Wrap(err, "Failed to convert s to string")
return 0, fmt.Errorf("Failed to convert s to string: %w", err)
}
return strings.Count(ss, substrs), nil
}

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
}

View File

@@ -4,7 +4,7 @@ import (
"bytes"
"strings"
"github.com/pkg/errors"
"errors"
"github.com/gohugoio/hugo/parser"
"github.com/gohugoio/hugo/parser/metadecoders"

View File

@@ -14,6 +14,7 @@
package transform
import (
"fmt"
"io/ioutil"
"strings"
@@ -23,9 +24,10 @@ import (
"github.com/mitchellh/mapstructure"
"errors"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
@@ -54,7 +56,7 @@ func (ns *Namespace) Unmarshal(args ...any) (any, error) {
data = args[1]
decoder, err = decodeDecoder(m)
if err != nil {
return nil, errors.WithMessage(err, "failed to decode options")
return nil, fmt.Errorf("failed to decode options: %w", err)
}
}
@@ -72,7 +74,7 @@ func (ns *Namespace) Unmarshal(args ...any) (any, error) {
return ns.cache.GetOrCreate(key, func() (any, error) {
f := metadecoders.FormatFromMediaType(r.MediaType())
if f == "" {
return nil, errors.Errorf("MIME %q not supported", r.MediaType())
return nil, fmt.Errorf("MIME %q not supported", r.MediaType())
}
reader, err := r.ReadSeekCloser()
@@ -92,7 +94,7 @@ func (ns *Namespace) Unmarshal(args ...any) (any, error) {
dataStr, err := types.ToStringE(data)
if err != nil {
return nil, errors.Errorf("type %T not supported", data)
return nil, fmt.Errorf("type %T not supported", data)
}
if dataStr == "" {
@@ -160,7 +162,7 @@ func stringToRune(v any) (rune, error) {
if i == 0 {
r = rr
} else {
return 0, errors.Errorf("invalid character: %q", v)
return 0, fmt.Errorf("invalid character: %q", v)
}
}

View File

@@ -22,7 +22,6 @@ import (
"github.com/gohugoio/hugo/common/urls"
"github.com/gohugoio/hugo/deps"
_errors "github.com/pkg/errors"
"github.com/spf13/cast"
)
@@ -55,7 +54,7 @@ func (ns *Namespace) AbsURL(s any) (template.HTML, error) {
func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
s, err := cast.ToStringE(rawurl)
if err != nil {
return nil, _errors.Wrap(err, "Error in Parse")
return nil, fmt.Errorf("Error in Parse: %w", err)
}
return url.Parse(s)