Skip to content

Commit 3f79f32

Browse files
committed
Revert error message changes
Changes in PR 65 modified error messages. While some of the changes read better, also produced duplicate prefixes (like "error converting YAML to JSON: error converting YAML to JSON:"). Even the improvements broke downstream unit tests relying on exact prefix matching. Reverting the error message changes before tagging a new release to avoid disrupting consumers until we consider the impact of these changes more thoroughly.
1 parent 65d71bb commit 3f79f32

File tree

2 files changed

+139
-6
lines changed

2 files changed

+139
-6
lines changed

err_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package yaml
18+
19+
import (
20+
"strings"
21+
"testing"
22+
)
23+
24+
func TestErrors(t *testing.T) {
25+
type Into struct {
26+
Map map[string]interface{} `json:"map"`
27+
Int int32 `json:"int"`
28+
}
29+
30+
testcases := []struct {
31+
Name string
32+
Data string
33+
UnmarshalPrefix string
34+
UnmarshalStrictPrefix string
35+
YAMLToJSONPrefix string
36+
YAMLToJSONStrictPrefix string
37+
}{
38+
{
39+
Name: "unmarshal syntax",
40+
Data: `map: {`,
41+
UnmarshalPrefix: `error converting YAML to JSON: yaml: line 1: `,
42+
UnmarshalStrictPrefix: `error converting YAML to JSON: yaml: line 1: `,
43+
YAMLToJSONPrefix: `yaml: line 1: `,
44+
YAMLToJSONStrictPrefix: `yaml: line 1: `,
45+
},
46+
{
47+
Name: "unmarshal type",
48+
Data: `map: ""`,
49+
UnmarshalPrefix: `error unmarshaling JSON: while decoding JSON: json: `,
50+
UnmarshalStrictPrefix: `error unmarshaling JSON: while decoding JSON: json: `,
51+
YAMLToJSONPrefix: ``,
52+
YAMLToJSONStrictPrefix: ``,
53+
},
54+
{
55+
Name: "unmarshal unknown",
56+
Data: `unknown: {}`,
57+
UnmarshalPrefix: ``,
58+
UnmarshalStrictPrefix: `error unmarshaling JSON: while decoding JSON: json: `,
59+
YAMLToJSONPrefix: ``,
60+
YAMLToJSONStrictPrefix: ``,
61+
},
62+
{
63+
Name: "unmarshal duplicate",
64+
Data: `
65+
int: 0
66+
int: 0`,
67+
UnmarshalPrefix: ``,
68+
UnmarshalStrictPrefix: `error converting YAML to JSON: yaml: `,
69+
YAMLToJSONPrefix: ``,
70+
YAMLToJSONStrictPrefix: `yaml: unmarshal errors:`,
71+
},
72+
}
73+
74+
for _, tc := range testcases {
75+
t.Run(tc.Name, func(t *testing.T) {
76+
v := Into{}
77+
if err := Unmarshal([]byte(tc.Data), &v); err == nil {
78+
if len(tc.UnmarshalPrefix) > 0 {
79+
t.Fatal("expected err")
80+
}
81+
} else {
82+
if len(tc.UnmarshalPrefix) == 0 {
83+
t.Fatalf("unexpected err %v", err)
84+
}
85+
if !strings.HasPrefix(err.Error(), tc.UnmarshalPrefix) {
86+
t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.UnmarshalPrefix)
87+
}
88+
}
89+
90+
if err := UnmarshalStrict([]byte(tc.Data), &v); err == nil {
91+
if len(tc.UnmarshalStrictPrefix) > 0 {
92+
t.Fatal("expected err")
93+
}
94+
} else {
95+
if len(tc.UnmarshalStrictPrefix) == 0 {
96+
t.Fatalf("unexpected err %v", err)
97+
}
98+
if !strings.HasPrefix(err.Error(), tc.UnmarshalStrictPrefix) {
99+
t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.UnmarshalStrictPrefix)
100+
}
101+
}
102+
103+
if _, err := YAMLToJSON([]byte(tc.Data)); err == nil {
104+
if len(tc.YAMLToJSONPrefix) > 0 {
105+
t.Fatal("expected err")
106+
}
107+
} else {
108+
if len(tc.YAMLToJSONPrefix) == 0 {
109+
t.Fatalf("unexpected err %v", err)
110+
}
111+
if !strings.HasPrefix(err.Error(), tc.YAMLToJSONPrefix) {
112+
t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.YAMLToJSONPrefix)
113+
}
114+
}
115+
116+
if _, err := YAMLToJSONStrict([]byte(tc.Data)); err == nil {
117+
if len(tc.YAMLToJSONStrictPrefix) > 0 {
118+
t.Fatal("expected err")
119+
}
120+
} else {
121+
if len(tc.YAMLToJSONStrictPrefix) == 0 {
122+
t.Fatalf("unexpected err %v", err)
123+
}
124+
if !strings.HasPrefix(err.Error(), tc.YAMLToJSONStrictPrefix) {
125+
t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.YAMLToJSONStrictPrefix)
126+
}
127+
}
128+
})
129+
}
130+
}

yaml.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func jsonUnmarshal(reader io.Reader, obj interface{}, opts ...JSONOpt) error {
9191
for _, opt := range opts {
9292
d = opt(d)
9393
}
94-
return d.Decode(obj)
94+
if err := d.Decode(&obj); err != nil {
95+
return fmt.Errorf("while decoding JSON: %v", err)
96+
}
97+
return nil
9598
}
9699

97100
// JSONToYAML converts JSON to YAML. Notable implementation details:
@@ -110,13 +113,13 @@ func JSONToYAML(j []byte) ([]byte, error) {
110113
// number type, so we can preserve number type throughout this process.
111114
err := yaml.Unmarshal(j, &jsonObj)
112115
if err != nil {
113-
return nil, fmt.Errorf("error converting JSON to YAML: %w", err)
116+
return nil, err
114117
}
115118

116119
// Marshal this object into YAML.
117120
yamlBytes, err := yaml.Marshal(jsonObj)
118121
if err != nil {
119-
return nil, fmt.Errorf("error converting JSON to YAML: %w", err)
122+
return nil, err
120123
}
121124

122125
return yamlBytes, nil
@@ -155,7 +158,7 @@ func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, unmarshalFn f
155158
var yamlObj interface{}
156159
err := unmarshalFn(yamlBytes, &yamlObj)
157160
if err != nil {
158-
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
161+
return nil, err
159162
}
160163

161164
// YAML objects are not completely compatible with JSON objects (e.g. you
@@ -164,13 +167,13 @@ func yamlToJSONTarget(yamlBytes []byte, jsonTarget *reflect.Value, unmarshalFn f
164167
// incompatibilties happen along the way.
165168
jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
166169
if err != nil {
167-
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
170+
return nil, err
168171
}
169172

170173
// Convert this object to JSON and return the data.
171174
jsonBytes, err := json.Marshal(jsonObj)
172175
if err != nil {
173-
return nil, fmt.Errorf("error converting YAML to JSON: %w", err)
176+
return nil, err
174177
}
175178
return jsonBytes, nil
176179
}

0 commit comments

Comments
 (0)