-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,10 @@ import ( | |
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/ghodss/yaml" | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this decoder used by edit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Better yet, make it say yaml or json appropriately. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 @Kargakis I think it is, but the error should be reporting appropriately for given format. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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