mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Improve nilpointer error message
This commit is contained in:
parent
4174a7866b
commit
8d42a7942a
3 changed files with 35 additions and 1 deletions
|
@ -19,8 +19,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -116,3 +118,22 @@ func IsNotExist(err error) bool {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nilPointerErrRe = regexp.MustCompile(`at <(.*)>: error calling (.*?): runtime error: invalid memory address or nil pointer dereference`)
|
||||||
|
|
||||||
|
func ImproveIfNilPointer(inErr error) (outErr error) {
|
||||||
|
outErr = inErr
|
||||||
|
|
||||||
|
m := nilPointerErrRe.FindStringSubmatch(inErr.Error())
|
||||||
|
if len(m) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
call := m[1]
|
||||||
|
field := m[2]
|
||||||
|
parts := strings.Split(call, ".")
|
||||||
|
receiverName := parts[len(parts)-2]
|
||||||
|
receiver := strings.Join(parts[:len(parts)-1], ".")
|
||||||
|
s := fmt.Sprintf("– %s is nil; wrap it in if or with: {{ with %s }}{{ .%s }}{{ end }}", receiverName, receiver, field)
|
||||||
|
outErr = errors.New(nilPointerErrRe.ReplaceAllString(inErr.Error(), s))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -170,3 +170,15 @@ Paginator: {{ .Paginator }}
|
||||||
b.Assert(err, qt.IsNotNil)
|
b.Assert(err, qt.IsNotNil)
|
||||||
b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for this page: kind: "page"`)
|
b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for this page: kind: "page"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNilPointerErrorMessage(t *testing.T) {
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
-- content/p1.md --
|
||||||
|
-- layouts/_default/single.html --
|
||||||
|
Home Filename: {{ site.Home.File.Filename }}
|
||||||
|
`
|
||||||
|
b, err := TestE(t, files)
|
||||||
|
b.Assert(err, qt.IsNotNil)
|
||||||
|
b.Assert(err.Error(), qt.Contains, `_default/single.html:1:22: executing "_default/single.html" – File is nil; wrap it in if or with: {{ with site.Home.File }}{{ .Filename }}{{ end }}`)
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/common/herrors"
|
||||||
"github.com/gohugoio/hugo/hugolib/doctree"
|
"github.com/gohugoio/hugo/hugolib/doctree"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/config"
|
"github.com/gohugoio/hugo/config"
|
||||||
|
@ -110,7 +111,7 @@ func (s *Site) renderPages(ctx *siteRenderContext) error {
|
||||||
|
|
||||||
err := <-errs
|
err := <-errs
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to render pages: %w", err)
|
return fmt.Errorf("failed to render pages: %w", herrors.ImproveIfNilPointer(err))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue