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
|
@ -17,6 +17,8 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/chaseadamsio/goorgeous"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -59,7 +61,7 @@ func unmarshal(data []byte, f Format, v interface{}) error {
|
|||
case ORG:
|
||||
vv, err := goorgeous.OrgHeaders(data)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to unmarshal ORG headers")
|
||||
return toFileError(f, errors.Wrap(err, "failed to unmarshal ORG headers"))
|
||||
}
|
||||
switch v.(type) {
|
||||
case *map[string]interface{}:
|
||||
|
@ -74,7 +76,7 @@ func unmarshal(data []byte, f Format, v interface{}) error {
|
|||
case YAML:
|
||||
err = yaml.Unmarshal(data, v)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to unmarshal YAML")
|
||||
return toFileError(f, errors.Wrap(err, "failed to unmarshal YAML"))
|
||||
}
|
||||
|
||||
// To support boolean keys, the YAML package unmarshals maps to
|
||||
|
@ -103,8 +105,16 @@ func unmarshal(data []byte, f Format, v interface{}) error {
|
|||
return errors.Errorf("unmarshal of format %q is not supported", f)
|
||||
}
|
||||
|
||||
return errors.Wrap(err, "unmarshal failed")
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return toFileError(f, errors.Wrap(err, "unmarshal failed"))
|
||||
|
||||
}
|
||||
|
||||
func toFileError(f Format, err error) error {
|
||||
return herrors.ToFileError(string(f), err)
|
||||
}
|
||||
|
||||
// stringifyMapKeys recurses into in and changes all instances of
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue