mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-14 20:33:59 +02: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:
@@ -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
|
||||
|
Reference in New Issue
Block a user