commands: Show server error info in browser

The main item in this commit is showing of errors with a file context when running `hugo server`.

This can be turned off: `hugo server --disableBrowserError` (can also be set in `config.toml`).

But to get there, the error handling in Hugo needed a revision. There are some items left TODO for commits soon to follow, most notable errors in content and config files.

Fixes #5284
Fixes #5290
See #5325
See #5324
This commit is contained in:
Bjørn Erik Pedersen
2018-10-03 14:58:09 +02:00
parent 3a3089121b
commit 35fbfb19a1
73 changed files with 1914 additions and 668 deletions

View File

@@ -16,7 +16,9 @@ package create
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"io"
"os"
"os/exec"
@@ -135,7 +137,7 @@ func newContentFromDir(
targetDir := filepath.Dir(targetFilename)
if err := targetFs.MkdirAll(targetDir, 0777); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create target directory for %s: %s", targetDir, err)
return errors.Wrapf(err, "failed to create target directory for %s:", targetDir)
}
out, err := targetFs.Create(targetFilename)
@@ -223,7 +225,7 @@ func mapArcheTypeDir(
func usesSiteVar(fs afero.Fs, filename string) (bool, error) {
f, err := fs.Open(filename)
if err != nil {
return false, fmt.Errorf("failed to open archetype file: %s", err)
return false, errors.Wrap(err, "failed to open archetype file")
}
defer f.Close()
return helpers.ReaderContains(f, []byte(".Site")), nil

View File

@@ -20,6 +20,8 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/source"
@@ -127,14 +129,14 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archety
templateHandler := s.Deps.Tmpl.(tpl.TemplateHandler)
templateName := "_text/" + helpers.Filename(archetypeFilename)
if err := templateHandler.AddTemplate(templateName, string(archetypeTemplate)); err != nil {
return nil, fmt.Errorf("Failed to parse archetype file %q: %s", archetypeFilename, err)
return nil, errors.Wrapf(err, "Failed to parse archetype file %q:", archetypeFilename)
}
templ, _ := templateHandler.Lookup(templateName)
var buff bytes.Buffer
if err := templ.Execute(&buff, data); err != nil {
return nil, fmt.Errorf("Failed to process archetype file %q: %s", archetypeFilename, err)
return nil, errors.Wrapf(err, "Failed to process archetype file %q:", archetypeFilename)
}
archetypeContent = []byte(archetypeShortcodeReplacementsPost.Replace(buff.String()))