commit d24769d1c4d6a3919992c1363c944ef40d82c8a9
parent f4d2152df71f2bb7cf58d71790d38ff5cd98ef8b
Author: gearsix <gearsix@tuta.io>
Date: Tue, 21 Feb 2023 14:26:59 +0000
data.go: made sure LoadData always indicates the format being parsed
Diffstat:
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,5 +1,9 @@
# CHANGELOG
+# v1.2.3
+
+- made sure the errors returned by `LoadData` and `WriteData` always indicates the DataFormat being parsed.
+
# v1.2.2
- Added standardised package errors:
diff --git a/data.go b/data.go
@@ -93,11 +93,15 @@ func LoadData(format DataFormat, in io.Reader, out interface{}) error {
switch format {
case JSON:
- err = json.Unmarshal(inbuf, out)
+ if err = json.Unmarshal(inbuf, out); err != nil {
+ err = fmt.Errorf("%s: %s", JSON, err.Error())
+ }
case YAML:
err = yaml.Unmarshal(inbuf, out)
case TOML:
- err = toml.Unmarshal(inbuf, out)
+ if err = toml.Unmarshal(inbuf, out); err != nil {
+ err = fmt.Errorf("%s %s", TOML, err.Error())
+ }
default:
err = ErrUnsupportedData(format.String())
}
@@ -124,11 +128,15 @@ func WriteData(format DataFormat, data interface{}, w io.Writer) error {
switch format {
case JSON:
- err = json.NewEncoder(w).Encode(data)
+ if err = json.NewEncoder(w).Encode(data); err != nil {
+ err = fmt.Errorf("%s: %s", JSON, err.Error())
+ }
case YAML:
err = yaml.NewEncoder(w).Encode(data)
case TOML:
- err = toml.NewEncoder(w).Encode(data)
+ if err = toml.NewEncoder(w).Encode(data); err != nil {
+ err = fmt.Errorf("%s %s", TOML, err.Error())
+ }
default:
err = ErrUnsupportedData(format.String())
}