errors: Misc improvements

* Redo the server error template
* Always add the content file context if relevant
* Remove some now superflous error string matching
* Move the server error template to _server/error.html
* Add file context (with position) to codeblock render blocks
* Improve JS build errors

Fixes #9892
Fixes #9891
Fixes #9893
This commit is contained in:
Bjørn Erik Pedersen
2022-05-12 11:43:20 +02:00
parent 4a96df96d9
commit 5c96bda70a
27 changed files with 600 additions and 204 deletions

View File

@@ -0,0 +1,87 @@
<!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", system-ui, -apple-system, "Segoe UI", Roboto,
"Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";
font-size: 14px;
background-color: #272a36;
}
main {
max-width: 100ch;
padding: 2ch;
margin: auto;
}
.version {
font-size: 0.75rem;
color: #7c7c7c;
}
hr {
margin-bottom: 1rem;
border: none;
height: 1px;
background-color: #3d3d3d;
}
pre,
code {
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
"Courier New", monospace;
}
.error pre {
line-height: 1.5;
}
.filename {
color: #eef78a;
font-size: 0.9rem;
line-height: 1.5;
}
.highlight {
overflow-x: auto;
}
a {
color: #0594cb;
text-decoration: none;
}
a:hover {
color: #ccc;
}
</style>
</head>
<body>
<main>
{{ $codeStyle := "dracula" }}
<div class="error">
{{ highlight .Error "apl" (printf "linenos=false,noclasses=true,style=%s" $codeStyle ) }}
</div>
<hr />
{{ range $i, $e := .Files }}
{{ if not .ErrorContext }}
{{ continue }}
{{ end }}
{{ $params := printf "noclasses=true,style=%s,linenos=table,hl_lines=%d,linenostart=%d" $codeStyle (add .ErrorContext.LinesPos 1) (sub .Position.LineNumber .ErrorContext.LinesPos) }}
{{ $lexer := .ErrorContext.ChromaLexer | default "go-html-template" }}
{{ with .Position }}
<code class="filename"
>{{ printf "%s:%d:%d" .Filename .LineNumber .ColumnNumber }}:</code
>
{{ end }}
{{ highlight (delimit .ErrorContext.Lines "\n") $lexer $params }}
<hr />
{{ end }}
<p class="version">{{ .Version }}</p>
<a href="">Reload Page</a>
</main>
</body>
</html>

View File

@@ -1,63 +0,0 @@
<!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

@@ -61,6 +61,7 @@ const (
// The identifiers may be truncated in the log, e.g.
// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
// We need this to identify position in templates with base templates applied.
var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`)
var embeddedTemplatesAliases = map[string][]string{
@@ -524,25 +525,27 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error
return inerr
}
identifiers := t.extractIdentifiers(inerr.Error())
//lint:ignore ST1008 the error is the main result
checkFilename := func(info templateInfo, inErr error) (error, bool) {
if info.filename == "" {
return inErr, false
}
lineMatcher := func(m herrors.LineMatcher) bool {
lineMatcher := func(m herrors.LineMatcher) int {
if m.Position.LineNumber != m.LineNumber {
return false
return -1
}
identifiers := t.extractIdentifiers(m.Error.Error())
for _, id := range identifiers {
if strings.Contains(m.Line, id) {
return true
// We found the line, but return a 0 to signal to
// use the column from the error message.
return 0
}
}
return false
return -1
}
f, err := t.layoutsFs.Open(info.filename)
@@ -551,7 +554,13 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error
}
defer f.Close()
return herrors.NewFileError(info.realFilename, inErr).UpdateContent(f, lineMatcher), true
fe := herrors.NewFileError(info.realFilename, inErr)
fe.UpdateContent(f, lineMatcher)
if !fe.ErrorContext().Position.IsValid() {
return inErr, false
}
return fe, true
}
inerr = fmt.Errorf("execute of template failed: %w", inerr)
@@ -565,6 +574,15 @@ func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error
return err
}
func (t *templateHandler) extractIdentifiers(line string) []string {
m := identifiersRe.FindAllStringSubmatch(line, -1)
identifiers := make([]string, len(m))
for i := 0; i < len(m); i++ {
identifiers[i] = m[i][1]
}
return identifiers
}
func (t *templateHandler) addShortcodeVariant(ts *templateState) {
name := ts.Name()
base := templateBaseName(templateShortcode, name)
@@ -721,18 +739,9 @@ func (t *templateHandler) applyTemplateTransformers(ns *templateNamespace, ts *t
return c, err
}
func (t *templateHandler) extractIdentifiers(line string) []string {
m := identifiersRe.FindAllStringSubmatch(line, -1)
identifiers := make([]string, len(m))
for i := 0; i < len(m); i++ {
identifiers[i] = m[i][1]
}
return identifiers
}
//go:embed embedded/templates/*
//go:embed embedded/templates/_default/*
//go:embed embedded/templates/server/*
//go:embed embedded/templates/_server/*
var embededTemplatesFs embed.FS
func (t *templateHandler) loadEmbedded() error {
@@ -755,7 +764,7 @@ func (t *templateHandler) loadEmbedded() error {
// 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") && !strings.HasPrefix(name, "server/") {
if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") {
templateName = internalPathPrefix + name
}

View File

@@ -59,6 +59,6 @@ func (info templateInfo) errWithFileContext(what string, err error) error {
return err
}
defer f.Close()
return fe.UpdateContent(f, herrors.SimpleLineMatcher)
return fe.UpdateContent(f, nil)
}