From 35fbfb19a173b01bc881f2bbc5d104136633a7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 3 Oct 2018 14:58:09 +0200 Subject: 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 --- resource/image.go | 6 ++++-- resource/postcss/postcss.go | 11 +++++------ resource/resource.go | 12 +++++------- resource/resource_metadata.go | 3 ++- resource/templates/execute_as_template.go | 5 ++--- resource/tocss/scss/tocss.go | 3 ++- resource/tocss/scss/tocss_notavailable.go | 4 ++-- resource/transform.go | 4 ++-- 8 files changed, 24 insertions(+), 24 deletions(-) (limited to 'resource') diff --git a/resource/image.go b/resource/image.go index e8b87cdb4..d9a1dd47d 100644 --- a/resource/image.go +++ b/resource/image.go @@ -26,6 +26,8 @@ import ( "strings" "sync" + _errors "github.com/pkg/errors" + "github.com/disintegration/imaging" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/helpers" @@ -430,7 +432,7 @@ func (i *Image) initConfig() error { }) if err != nil { - return fmt.Errorf("failed to load image config: %s", err) + return _errors.Wrap(err, "failed to load image config") } return nil @@ -439,7 +441,7 @@ func (i *Image) initConfig() error { func (i *Image) decodeSource() (image.Image, error) { f, err := i.ReadSeekCloser() if err != nil { - return nil, fmt.Errorf("failed to open image for decode: %s", err) + return nil, _errors.Wrap(err, "failed to open image for decode") } defer f.Close() img, _, err := image.Decode(f) diff --git a/resource/postcss/postcss.go b/resource/postcss/postcss.go index 202b4c06b..ec73543dd 100644 --- a/resource/postcss/postcss.go +++ b/resource/postcss/postcss.go @@ -14,19 +14,18 @@ package postcss import ( - "fmt" "io" "path/filepath" "github.com/gohugoio/hugo/hugofs" + "github.com/pkg/errors" - "github.com/mitchellh/mapstructure" - // "io/ioutil" "os" "os/exec" - "github.com/gohugoio/hugo/common/errors" + "github.com/mitchellh/mapstructure" + "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/resource" ) @@ -111,7 +110,7 @@ func (t *postcssTransformation) Transform(ctx *resource.ResourceTransformationCt binary = binaryName if _, err := exec.LookPath(binary); err != nil { // This may be on a CI server etc. Will fall back to pre-built assets. - return errors.ErrFeatureNotAvailable + return herrors.ErrFeatureNotAvailable } } @@ -134,7 +133,7 @@ func (t *postcssTransformation) Transform(ctx *resource.ResourceTransformationCt if err != nil { if t.options.Config != "" { // Only fail if the user specificed config file is not found. - return fmt.Errorf("postcss config %q not found: %s", configFile, err) + return errors.Wrapf(err, "postcss config %q not found:", configFile) } configFile = "" } else { diff --git a/resource/resource.go b/resource/resource.go index dd9cbbd41..be3ebdb8b 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -14,7 +14,6 @@ package resource import ( - "errors" "fmt" "io" "io/ioutil" @@ -27,13 +26,12 @@ import ( "github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/tpl" + "github.com/pkg/errors" "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/loggers" - jww "github.com/spf13/jwalterweatherman" - "github.com/spf13/afero" "github.com/gobwas/glob" @@ -273,7 +271,7 @@ type Spec struct { MediaTypes media.Types OutputFormats output.Formats - Logger *jww.Notepad + Logger *loggers.Logger TextTemplates tpl.TemplateParseFinder @@ -287,7 +285,7 @@ type Spec struct { GenAssetsPath string } -func NewSpec(s *helpers.PathSpec, logger *jww.Notepad, outputFormats output.Formats, mimeTypes media.Types) (*Spec, error) { +func NewSpec(s *helpers.PathSpec, logger *loggers.Logger, outputFormats output.Formats, mimeTypes media.Types) (*Spec, error) { imaging, err := decodeImaging(s.Cfg.GetStringMap("imaging")) if err != nil { @@ -542,7 +540,7 @@ type resourceHash struct { type publishOnce struct { publisherInit sync.Once publisherErr error - logger *jww.Notepad + logger *loggers.Logger } func (l *publishOnce) publish(s Source) error { @@ -660,7 +658,7 @@ func (l *genericResource) initHash() error { var f hugio.ReadSeekCloser f, err = l.ReadSeekCloser() if err != nil { - err = fmt.Errorf("failed to open source file: %s", err) + err = errors.Wrap(err, "failed to open source file") return } defer f.Close() diff --git a/resource/resource_metadata.go b/resource/resource_metadata.go index 2c82aeaf6..20c4f130b 100644 --- a/resource/resource_metadata.go +++ b/resource/resource_metadata.go @@ -17,6 +17,7 @@ import ( "fmt" "strconv" + "github.com/pkg/errors" "github.com/spf13/cast" "strings" @@ -69,7 +70,7 @@ func AssignMetadata(metadata []map[string]interface{}, resources ...Resource) er glob, err := getGlob(srcKey) if err != nil { - return fmt.Errorf("failed to match resource with metadata: %s", err) + return errors.Wrap(err, "failed to match resource with metadata") } match := glob.Match(resourceSrcKey) diff --git a/resource/templates/execute_as_template.go b/resource/templates/execute_as_template.go index dee9d0d9a..a126b26c9 100644 --- a/resource/templates/execute_as_template.go +++ b/resource/templates/execute_as_template.go @@ -15,11 +15,10 @@ package templates import ( - "fmt" - "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/resource" "github.com/gohugoio/hugo/tpl" + "github.com/pkg/errors" ) // Client contains methods to perform template processing of Resource objects. @@ -55,7 +54,7 @@ func (t *executeAsTemplateTransform) Transform(ctx *resource.ResourceTransformat tplStr := helpers.ReaderToString(ctx.From) templ, err := t.textTemplate.Parse(ctx.InPath, tplStr) if err != nil { - return fmt.Errorf("failed to parse Resource %q as Template: %s", ctx.InPath, err) + return errors.Wrapf(err, "failed to parse Resource %q as Template:", ctx.InPath) } ctx.OutPath = t.targetPath diff --git a/resource/tocss/scss/tocss.go b/resource/tocss/scss/tocss.go index 5ba7793c0..984e14fc2 100644 --- a/resource/tocss/scss/tocss.go +++ b/resource/tocss/scss/tocss.go @@ -29,6 +29,7 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/resource" + "github.com/pkg/errors" ) // Used in tests. This feature requires Hugo to be built with the extended tag. @@ -165,7 +166,7 @@ func (c *Client) toCSS(options scss.Options, dst io.Writer, src io.Reader) (tocs res, err = transpiler.Execute(dst, src) if err != nil { - return res, fmt.Errorf("SCSS processing failed: %s", err) + return res, errors.Wrap(err, "SCSS processing failed") } return res, nil diff --git a/resource/tocss/scss/tocss_notavailable.go b/resource/tocss/scss/tocss_notavailable.go index 2ec5a4832..df918b368 100644 --- a/resource/tocss/scss/tocss_notavailable.go +++ b/resource/tocss/scss/tocss_notavailable.go @@ -16,7 +16,7 @@ package scss import ( - "github.com/gohugoio/hugo/common/errors" + "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/resource" ) @@ -26,5 +26,5 @@ func Supports() bool { } func (t *toCSSTransformation) Transform(ctx *resource.ResourceTransformationCtx) error { - return errors.ErrFeatureNotAvailable + return herrors.ErrFeatureNotAvailable } diff --git a/resource/transform.go b/resource/transform.go index 01b05b73e..d3a215467 100644 --- a/resource/transform.go +++ b/resource/transform.go @@ -20,7 +20,7 @@ import ( "strings" "github.com/gohugoio/hugo/common/collections" - "github.com/gohugoio/hugo/common/errors" + "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/helpers" "github.com/mitchellh/hashstructure" @@ -390,7 +390,7 @@ func (r *transformedResource) transform(setContent bool) (err error) { } if err := tr.transformation.Transform(tctx); err != nil { - if err == errors.ErrFeatureNotAvailable { + if err == herrors.ErrFeatureNotAvailable { // This transformation is not available in this // Hugo installation (scss not compiled in, PostCSS not available etc.) // If a prepared bundle for this transformation chain is available, use that. -- cgit v1.2.3