Skip to content

Commit

Permalink
encoding/json: add "json: " prefix to SyntaxError messages
Browse files Browse the repository at this point in the history
The other named errors - UnmarshalTypeError, etc - in this package do
the same, so we should prepend the package prefix to error messages
for consistency.

Add a note to the release docs in case this is interpreted as
a breaking change.

Fixes #36221.

Change-Id: Ie24b532bbf9812e108c259fa377e2a6b64319ed4
Reviewed-on: https://go-review.googlesource.com/c/go/+/263619
Run-TryBot: Kevin Burke <kev@inburke.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Kevin Burke <kev@inburke.com>
Trust: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
kevinburke committed Oct 21, 2020
1 parent 612a363 commit 6af088b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
8 changes: 8 additions & 0 deletions doc/go1.16.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ <h3 id="crypto/x509"><a href="/pkg/crypto/x509">crypto/x509</a></h3>
of a malformed certificate.
</p>

<h3 id="encoding/json"><a href="/pkg/encoding/json">encoding/json</a></h3>

<p><!-- CL 263619 -->
The error message for
<a href="/pkg/encoding/json/#SyntaxError">SyntaxError</a>
now begins with "json: ", matching the other errors in the package.
</p>

<h3 id="net"><a href="/pkg/net/">net</a></h3>

<p><!-- CL 250357 -->
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/testdata/script/mod_proxy_invalid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ env GO111MODULE=on
env GOPROXY=$GOPROXY/invalid

! go list -m rsc.io/quote@latest
stderr '^go list -m: module rsc.io/quote: invalid response from proxy "'$GOPROXY'": invalid character ''i'' looking for beginning of value$'
stderr '^go list -m: module rsc.io/quote: invalid response from proxy "'$GOPROXY'": json: invalid character ''i'' looking for beginning of value$'

! go list -m rsc.io/quote@1.5.2
stderr '^go list -m: rsc.io/quote@1.5.2: invalid version: invalid response from proxy "'$GOPROXY'": invalid character ''i'' looking for beginning of value$'
stderr '^go list -m: rsc.io/quote@1.5.2: invalid version: invalid response from proxy "'$GOPROXY'": json: invalid character ''i'' looking for beginning of value$'
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_query_empty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ env GOPROXY=file:///$WORK/gatekeeper
chmod 0000 $WORK/gatekeeper/example.com/join/subpkg/@latest
cp go.mod.orig go.mod
! go get -d example.com/join/subpkg
stderr 'go get example.com/join/subpkg: module example.com/join/subpkg: (invalid response from proxy ".+": invalid character .+|reading file://.*/gatekeeper/example.com/join/subpkg/@latest: .+)'
stderr 'go get example.com/join/subpkg: module example.com/join/subpkg: (invalid response from proxy ".+": json: invalid character .+|reading file://.*/gatekeeper/example.com/join/subpkg/@latest: .+)'

-- go.mod.orig --
module example.com/othermodule
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/json/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type SyntaxError struct {
Offset int64 // error occurred after reading Offset bytes
}

func (e *SyntaxError) Error() string { return e.msg }
func (e *SyntaxError) Error() string { return "json: " + e.msg }

// A scanner is a JSON scanning state machine.
// Callers call scan.reset and then pass bytes in one at a time
Expand Down
2 changes: 1 addition & 1 deletion src/html/template/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestEscape(t *testing.T) {
{
"badMarshaler",
`<button onclick='alert(1/{{.B}}in numbers)'>`,
`<button onclick='alert(1/ /* json: error calling MarshalJSON for type *template.badMarshaler: invalid character &#39;f&#39; looking for beginning of object key string */null in numbers)'>`,
`<button onclick='alert(1/ /* json: error calling MarshalJSON for type *template.badMarshaler: json: invalid character &#39;f&#39; looking for beginning of object key string */null in numbers)'>`,
},
{
"jsMarshaler",
Expand Down

0 comments on commit 6af088b

Please sign in to comment.