Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return typed json,yaml syntax errors #38905

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ func (d *YAMLToJSONDecoder) Decode(into interface{}) error {
}

if len(bytes) != 0 {
data, err := yaml.YAMLToJSON(bytes)
err := yaml.Unmarshal(bytes, into)
if err != nil {
return err
return YAMLSyntaxError{err}
}
return json.Unmarshal(data, into)
}
return err
}
Expand Down Expand Up @@ -184,6 +183,23 @@ type YAMLOrJSONDecoder struct {
rawData []byte
}

type JSONSyntaxError struct {
Line int
Err error
}

func (e JSONSyntaxError) Error() string {
return fmt.Sprintf("json: line %d: %s", e.Line, e.Err.Error())
}

type YAMLSyntaxError struct {
err error
}

func (e YAMLSyntaxError) Error() string {
return e.err.Error()
}

// NewYAMLOrJSONDecoder returns a decoder that will process YAML documents
// or JSON documents from the given reader as a stream. bufferSize determines
// how far into the stream the decoder will look to figure out whether this
Expand Down Expand Up @@ -226,7 +242,10 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {

start := strings.LastIndex(js[:syntax.Offset], "\n") + 1
line := strings.Count(js[:start], "\n")
return fmt.Errorf("json: line %d: %s", line, syntax.Error())
return JSONSyntaxError{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine.

Line: line,
Err: fmt.Errorf(syntax.Error()),
}
}
}
return err
Expand Down
33 changes: 33 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,36 @@ func testReadLines(t *testing.T, lineLengths []int) {
}
}
}

func TestTypedJSONOrYamlErrors(t *testing.T) {
s := NewYAMLOrJSONDecoder(bytes.NewReader([]byte(`{
"foo": {
"stuff": 1
"otherStuff": 2
}
}
`)), 100)
obj := generic{}
err := s.Decode(&obj)
if err == nil {
t.Fatal("expected error with json: prefix, got no error")
}
if _, ok := err.(JSONSyntaxError); !ok {
t.Fatalf("expected %q to be of type JSONSyntaxError", err.Error())
}

s = NewYAMLOrJSONDecoder(bytes.NewReader([]byte(`---
stuff: 1
test-foo: 1

---
`)), 100)
obj = generic{}
err = s.Decode(&obj)
if err == nil {
t.Fatal("expected error with yaml: prefix, got no error")
}
if _, ok := err.(YAMLSyntaxError); !ok {
t.Fatalf("expected %q to be of type YAMLSyntaxError", err.Error())
}
}