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

Display line number on JSON errors #25038

Merged
merged 1 commit into from
May 10, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkg/util/yaml/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"unicode"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -203,7 +206,20 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
d.decoder = NewYAMLToJSONDecoder(buffer)
}
}
return d.decoder.Decode(into)
err := d.decoder.Decode(into)
if jsonDecoder, ok := d.decoder.(*json.Decoder); ok {
Copy link
Member

Choose a reason for hiding this comment

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

How does this work for yaml files? Can you add tests demonstrating that it works and reports the right lines for both json and yaml input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lavalamp yes, working on them

if syntax, ok := err.(*json.SyntaxError); ok {
data, readErr := ioutil.ReadAll(jsonDecoder.Buffered())
if readErr != nil {
glog.V(4).Infof("reading stream failed: %v", readErr)
}
js := string(data)
start := strings.LastIndex(js[:syntax.Offset], "\n") + 1
line := strings.Count(js[:start], "\n")
return fmt.Errorf("json: line %d: %s", line, syntax.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can I get this error while editing a yaml file for example? If so, drop the json prefix.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this decoder used by edit?

Copy link
Member

Choose a reason for hiding this comment

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

If so, drop the json prefix.

Better yet, make it say yaml or json appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lavalamp @Kargakis I added 2 unit tests for broken YAML and broken JSON. The NewYAMLOrJSONDecoder seems to do good job about deciding what parser will be used and what prefix the error line will get. I think the only difference is that the YAML parser reports errors natively where the JSON not. The error above matches the YAML parser for consistency.

@Kargakis I think it is, but the error should be reporting appropriately for given format.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. I missed the part where you typecast to a json decoder.

}
}
return err
}

// GuessJSONStream scans the provided reader up to size, looking
Expand Down
36 changes: 36 additions & 0 deletions pkg/util/yaml/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"testing"
)

Expand Down Expand Up @@ -125,6 +126,41 @@ stuff: 1
}
}

func TestDecodeBrokenYAML(t *testing.T) {
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 !strings.HasPrefix(err.Error(), "yaml: line 2:") {
t.Fatalf("expected %q to have 'yaml: line 2:' prefix", err.Error())
}
}

func TestDecodeBrokenJSON(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 !strings.HasPrefix(err.Error(), "json: line 3:") {
t.Fatalf("expected %q to have 'json: line 3:' prefix", err.Error())
}
}

type generic map[string]interface{}

func TestYAMLOrJSONDecoder(t *testing.T) {
Expand Down