mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 14:10:31 +03:00
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
This commit is contained in:
parent
ed7b3e2619
commit
f669ef6bec
10 changed files with 228 additions and 119 deletions
|
@ -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)
|
||||
|
@ -170,6 +169,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,
|
||||
|
@ -246,6 +259,16 @@ description = "Descriptioon"
|
|||
Some content.
|
||||
|
||||
|
||||
`))
|
||||
|
||||
b.WithContent("myjson.md", f(tomlcontent, `{
|
||||
"title": "This is a title",
|
||||
"description": "This is a description."
|
||||
}
|
||||
|
||||
Some content.
|
||||
|
||||
|
||||
`))
|
||||
|
||||
createErr := b.CreateSitesE()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue