diff options
| author | Eugene Kalinin <e.v.kalinin@gmail.com> | 2019-06-28 23:59:49 +0300 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2019-10-16 22:58:08 +0000 |
| commit | 02196d36575636a64f868ee0ffe6bb61442e7245 (patch) | |
| tree | 79a31755b7516b27eef4ec7e0c76bfead383633a /src/encoding/json/encode_test.go | |
| parent | 943df4f629560f5c33474dd82e2b534ea5f8653f (diff) | |
| download | go-02196d36575636a64f868ee0ffe6bb61442e7245.tar.xz | |
encoding/json: correct caller's name in encoding errors
1. Change mapencode.encode to use fmt.Error rather than MarshalerError.
MarshalerError refer to MarshalJSON, but mapencode.encode does not use that.
2. Add sourceFunc field to MarshalerError to record the name of the function
that creates the error, so that the Error method can report it correctly.
Fixes #29753
Change-Id: I186c2fac8470ae2f9e300501de3730face642230
Reviewed-on: https://go-review.googlesource.com/c/go/+/184119
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/encoding/json/encode_test.go')
| -rw-r--r-- | src/encoding/json/encode_test.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/encoding/json/encode_test.go b/src/encoding/json/encode_test.go index 8d3503b1ba..40f16d86ff 100644 --- a/src/encoding/json/encode_test.go +++ b/src/encoding/json/encode_test.go @@ -1064,3 +1064,30 @@ func TestMarshalUncommonFieldNames(t *testing.T) { t.Fatalf("Marshal: got %s want %s", got, want) } } + +func TestMarshalerError(t *testing.T) { + s := "test variable" + st := reflect.TypeOf(s) + errText := "json: test error" + + tests := []struct { + err *MarshalerError + want string + }{ + { + &MarshalerError{st, fmt.Errorf(errText), ""}, + "json: error calling MarshalJSON for type " + st.String() + ": " + errText, + }, + { + &MarshalerError{st, fmt.Errorf(errText), "TestMarshalerError"}, + "json: error calling TestMarshalerError for type " + st.String() + ": " + errText, + }, + } + + for i, tt := range tests { + got := tt.err.Error() + if got != tt.want { + t.Errorf("MarshalerError test %d, got: %s, want: %s", i, got, tt.want) + } + } +} |
