diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-23 08:54:10 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-23 14:35:43 +0200 |
commit | f669ef6bec25155d015b6ab231c53caef4fa5cdc (patch) | |
tree | a76f3843a7249ccbc61ec6c8a20ac2c38e8518cc /hugolib | |
parent | ed7b3e261909fe425ef64216f12806840c45b205 (diff) |
herrors: Improve handling of JSON errors
`*json.UnmarshalTypeError` and `*json.SyntaxError` has a byte `Offset`, so use that.
This commit also reworks/simplifies the errror line matching logic. This also makes the file reading unbuffered, but that should be fine in this error case.
See #5324
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/hugo_sites.go | 2 | ||||
-rw-r--r-- | hugolib/hugo_sites_build_errors_test.go | 27 | ||||
-rw-r--r-- | hugolib/page_content.go | 8 |
3 files changed, 32 insertions, 5 deletions
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index a184e8877..65e3260f6 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -1,4 +1,4 @@ -// Copyright 2016-present The Hugo Authors. All rights reserved. +// Copyright 2018 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/hugolib/hugo_sites_build_errors_test.go b/hugolib/hugo_sites_build_errors_test.go index f290022e0..1e53eb3c4 100644 --- a/hugolib/hugo_sites_build_errors_test.go +++ b/hugolib/hugo_sites_build_errors_test.go @@ -150,8 +150,7 @@ func TestSiteBuildErrors(t *testing.T) { name: "Invalid YAML front matter", fileType: yamlcontent, fileFixer: func(content string) string { - // TODO(bep) 2errors YAML line numbers seems to be off by one for > 1 line. - return strings.Replace(content, "title:", "title", 1) + return strings.Replace(content, "title:", "title: %foo", 1) }, assertBuildError: func(a testSiteBuildErrorAsserter, err error) { a.assertLineNumber(2, err) @@ -171,6 +170,20 @@ func TestSiteBuildErrors(t *testing.T) { }, }, { + name: "Invalid JSON front matter", + fileType: tomlcontent, + fileFixer: func(content string) string { + return strings.Replace(content, "\"description\":", "\"description\"", 1) + }, + assertBuildError: func(a testSiteBuildErrorAsserter, err error) { + fe := a.getFileError(err) + + assert.Equal(3, fe.LineNumber) + assert.Equal("json", fe.ErrorContext.ChromaLexer) + + }, + }, + { name: "Panic in template Execute", fileType: single, fileFixer: func(content string) string { @@ -248,6 +261,16 @@ Some content. `)) + b.WithContent("myjson.md", f(tomlcontent, `{ + "title": "This is a title", + "description": "This is a description." +} + +Some content. + + +`)) + createErr := b.CreateSitesE() if test.assertCreateError != nil { test.assertCreateError(errorAsserter, createErr) diff --git a/hugolib/page_content.go b/hugolib/page_content.go index 8c20db761..be015253b 100644 --- a/hugolib/page_content.go +++ b/hugolib/page_content.go @@ -89,7 +89,11 @@ Loop: f := metadecoders.FormatFromFrontMatterType(it.Type) m, err := metadecoders.UnmarshalToMap(it.Val, f) if err != nil { - return herrors.ToFileErrorWithOffset(string(f), err, iter.LineNumber()-1) + if fe, ok := err.(herrors.FileError); ok { + return herrors.ToFileErrorWithOffset(fe, iter.LineNumber()-1) + } else { + return err + } } if err := p.updateMetaData(m); err != nil { return err @@ -192,6 +196,6 @@ func parseError(err error, input []byte, pos int) error { input = input[:pos] lineNumber := bytes.Count(input, lf) + 1 endOfLastLine := bytes.LastIndex(input, lf) - return herrors.NewFileError("md", lineNumber, pos-endOfLastLine, err) + return herrors.NewFileError("md", -1, lineNumber, pos-endOfLastLine, err) } |