Skip to content

Commit

Permalink
Merge pull request #25038 from mfojtik/json-line
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Display line number on JSON errors

Related issue: #12231

This PR will introduce line numbers for all JSON errors in the CLI:

(this is existing error reporting for YAML)
```console
$ kubectl create -f broken.yaml
yaml: line 8: mapping values are not allowed in this context
```

(this is error reporting proposed in this PR for JSON)
```console
$ kubectl create -f broken.json
json: line 35: invalid character '{' after object key
```

(and this is the current reporting:)
```console
$ kubectl create -f broken.json
invalid character '{' after object key
```
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()
  • Loading branch information
k8s-merge-robot committed May 10, 2016
2 parents c7343fc + 543a8b2 commit 5ca769e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
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 {
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())
}
}
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

0 comments on commit 5ca769e

Please sign in to comment.