diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-21 12:20:21 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-22 20:46:14 +0200 |
commit | d1661b823af25c50d3bbe5366ea40a3cdd52e237 (patch) | |
tree | cd84d18229fb9c294ff1be56d7c0ce92a8f46761 /hugolib/page_content.go | |
parent | 7930d2132a3c36c1aaca20f16f56978c84656b0a (diff) |
hugolib: Continue the file context/line number errors work
See #5324
Diffstat (limited to 'hugolib/page_content.go')
-rw-r--r-- | hugolib/page_content.go | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/hugolib/page_content.go b/hugolib/page_content.go index 39abd0981..8c20db761 100644 --- a/hugolib/page_content.go +++ b/hugolib/page_content.go @@ -14,11 +14,14 @@ package hugolib import ( - "fmt" + "bytes" "io" + errors "github.com/pkg/errors" + bp "github.com/gohugoio/hugo/bufferpool" + "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/parser/metadecoders" "github.com/gohugoio/hugo/parser/pageparser" ) @@ -31,11 +34,6 @@ var ( type pageContent struct { renderable bool - frontmatter []byte - - // rawContent is the raw content read from the content file. - rawContent []byte - // workContent is a copy of rawContent that may be mutated during site build. workContent []byte @@ -66,6 +64,10 @@ func (p *Page) mapContent() error { iter := p.source.parsed.Iterator() + fail := func(err error, i pageparser.Item) error { + return parseError(err, iter.Input(), i.Pos) + } + // the parser is guaranteed to return items in proper order or fail, so … // … it's safe to keep some "global" state var currShortcode shortcode @@ -87,7 +89,7 @@ Loop: f := metadecoders.FormatFromFrontMatterType(it.Type) m, err := metadecoders.UnmarshalToMap(it.Val, f) if err != nil { - return err + return herrors.ToFileErrorWithOffset(string(f), err, iter.LineNumber()-1) } if err := p.updateMetaData(m); err != nil { return err @@ -125,7 +127,7 @@ Loop: } if err != nil { - return err + return fail(errors.Wrap(err, "failed to extract shortcode"), it) } if currShortcode.params == nil { @@ -139,10 +141,10 @@ Loop: case it.IsEOF(): break Loop case it.IsError(): - err := fmt.Errorf("%s:shortcode:%d: %s", - p.pathOrTitle(), iter.LineNumber(), it) + err := fail(errors.WithStack(errors.New(it.ValStr())), it) currShortcode.err = err return err + default: result.Write(it.Val) } @@ -180,3 +182,16 @@ func (p *Page) parse(reader io.Reader) error { return nil } + +func parseError(err error, input []byte, pos int) error { + if herrors.UnwrapFileError(err) != nil { + // Use the most specific location. + return err + } + lf := []byte("\n") + input = input[:pos] + lineNumber := bytes.Count(input, lf) + 1 + endOfLastLine := bytes.LastIndex(input, lf) + return herrors.NewFileError("md", lineNumber, pos-endOfLastLine, err) + +} |