Skip to content

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

Open
@kevinburke1

Description

@kevinburke1

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions