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

Conversation

mfojtik
Copy link
Contributor

@mfojtik mfojtik commented May 2, 2016

Related issue: #12231

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

(this is existing error reporting for YAML)

$ 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)

$ kubectl create -f broken.json
json: line 35: invalid character '{' after object key

(and this is the current reporting:)

$ kubectl create -f broken.json
invalid character '{' after object key

Analytics

@mfojtik
Copy link
Contributor Author

mfojtik commented May 2, 2016

@smarterclayton @Kargakis PTAL

@k8s-github-robot k8s-github-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. release-note-label-needed labels May 2, 2016
@smarterclayton
Copy link
Contributor

Quick unit test please

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.

@mfojtik mfojtik force-pushed the json-line branch 2 times, most recently from 266ac4c to ce8ba42 Compare May 3, 2016 09:04
@k8s-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 3, 2016
@mfojtik
Copy link
Contributor Author

mfojtik commented May 3, 2016

@smarterclayton unit test added.

if err == nil {
t.Fatal("expected error with json: prefix, got no error")
}
if !strings.HasPrefix(err.Error(), "yaml: line 1:") {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this error be on line 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it looks so, but I haven't touched the yaml parser... it seems the bug is in yaml.... I will create follow up issue?

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 actually I think the problem was tabs on the second line, I fixed this case... however yaml still reports line '2' where it should be '3' (I guess it does not count '---' as 'line'.

Copy link
Contributor

Choose a reason for hiding this comment

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

It does not

On Fri, May 6, 2016 at 7:12 AM, Michal Fojtik notifications@github.com
wrote:

In pkg/util/yaml/decoder_test.go
#25038 (comment)
:

@@ -125,6 +126,40 @@ stuff: 1
}
}

+func TestDecodeBrokenYAML(t *testing.T) {

  • s := NewYAMLOrJSONDecoder(bytes.NewReader([]byte(`---
  • stuff:
    +broken-indent: 1
    +---
  • `)), 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(), "yaml: line 1:") {

@lavalamp https://github.com/lavalamp actually I think the problem was
tabs on the second line, I fixed this case... however yaml still reports
line '2' where it should be '3' (I guess it does not count '---' as 'line'.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/kubernetes/kubernetes/pull/25038/files/ce8ba423ddf64050c4daed6548fa8794e2a4e282#r62316695

@mfojtik
Copy link
Contributor Author

mfojtik commented May 9, 2016

@smarterclayton bump

@bgrant0607
Copy link
Member

cc @kubernetes/kubectl

@bgrant0607 bgrant0607 added area/kubectl ok-to-merge release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed needs-ok-to-merge labels May 9, 2016
@0xmichalis
Copy link
Contributor

LGTM

@lavalamp may want to have a look

@0xmichalis 0xmichalis removed their assignment May 9, 2016
@bgrant0607 bgrant0607 added the priority/backlog Higher priority than priority/awaiting-more-evidence. label May 9, 2016
@lavalamp
Copy link
Member

lavalamp commented May 9, 2016

LGTM

@lavalamp lavalamp added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 9, 2016
@k8s-bot
Copy link

k8s-bot commented May 9, 2016

GCE e2e build/test passed for commit 543a8b2.

@k8s-github-robot
Copy link

@k8s-bot test this [submit-queue is verifying that this PR is safe to merge]

@k8s-bot
Copy link

k8s-bot commented May 10, 2016

GCE e2e build/test passed for commit 543a8b2.

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 5ca769e into kubernetes:master May 10, 2016
openshift-publish-robot pushed a commit to openshift/kubernetes that referenced this pull request Jun 4, 2020
…to-release-4.2

Bug 1840872: UPSTREAM: 88440: Report deleted pod status correctly

Origin-commit: 77869edca67678ff9f6575094bae0ede1314f989
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants