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

encoding/json: SyntaxError does not have "json:" prefix #36221

Open
kevinburkemeter opened this issue Dec 19, 2019 · 3 comments
Open

encoding/json: SyntaxError does not have "json:" prefix #36221

kevinburkemeter opened this issue Dec 19, 2019 · 3 comments
Labels
Milestone

Comments

@kevinburkemeter
Copy link

@kevinburkemeter kevinburkemeter commented Dec 19, 2019

It was my understanding that most errors created inside of a standard library package should be prefixed with the package name. For example, the error message for sql.ErrNoRows is errors.New("sql: no rows in result set"), not errors.New("no rows in result set").

Some errors in the json package are returned without a leading prefix. I noticed this because I hit one and it took some digging to figure out where in the codebase the error was actually coming from.

These tests in scanner_test.go check that the error matches a SyntaxError without a leading prefix.

var indentErrorTests = []indentErrorTest{
	{`{"X": "foo", "Y"}`, &SyntaxError{"invalid character '}' after object key", 17}},
	{`{"X": "foo" "Y": "bar"}`, &SyntaxError{"invalid character '\"' after object key:value pair", 13}},
}

func TestIndentErrors(t *testing.T) {
	for i, tt := range indentErrorTests {
		slice := make([]uint8, 0)
		buf := bytes.NewBuffer(slice)
		if err := Indent(buf, []uint8(tt.in), "", ""); err != nil {
			if !reflect.DeepEqual(err, tt.err) {
				t.Errorf("#%d: Indent: %#v", i, err)
				continue
			}
		}
	}
}

Is that right? Shouldn't those error messages have a leading "json:" prefix?

@kevinburkemeter
Copy link
Author

@kevinburkemeter kevinburkemeter commented Dec 19, 2019

I can submit a fix, just let me know if that's something it would be good to fix

@mvdan
Copy link
Member

@mvdan mvdan commented Dec 19, 2019

I assume you mean that their Error() string methods should begin with json: . I'd probably agree there, though I haven't taken a closer look at why the errors in the package aren't stringified in a consistent way.

I'm not sure that the prefix should be part of the error value itself, though. If the error type is json.SyntaxError, having its message also contain json: seems a bit redundant. The fmt.Errorf errors do contain the prefix, but that's just because they are generic errors.

@mvdan mvdan added the NeedsDecision label Dec 19, 2019
@dmitshur dmitshur added this to the Backlog milestone Dec 20, 2019
@dpinela
Copy link
Contributor

@dpinela dpinela commented Dec 28, 2019

I've been thrown off by this before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.