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 /parser/metadecoders/decoder.go | |
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 'parser/metadecoders/decoder.go')
-rw-r--r-- | parser/metadecoders/decoder.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go index 0cb6afa5b..47d8af912 100644 --- a/parser/metadecoders/decoder.go +++ b/parser/metadecoders/decoder.go @@ -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 |