Skip to content

Commit dcbf2a9

Browse files
uhthomasstamblerre
authored andcommitted
internal/jsonrpc2: omit empty error data
The spec states error data may be omitted. It is currently always encoded as null, despite having no usage. Omit the field if empty, and add a test to prove the behaviour. Fixes golang/go#39736 Change-Id: Icdb39409010f3a42f84d2372c2061e4bc7cc198e Reviewed-on: https://go-review.googlesource.com/c/tools/+/239059 Reviewed-by: Ian Cottrell <iancottrell@google.com>
1 parent f8e0ea3 commit dcbf2a9

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

internal/jsonrpc2/wire.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type wireError struct {
8181
// Message is a short description of the error.
8282
Message string `json:"message"`
8383
// Data is optional structured data containing additional information about the error.
84-
Data *json.RawMessage `json:"data"`
84+
Data *json.RawMessage `json:"data,omitempty"`
8585
}
8686

8787
// wireVersionTag is a special 0 sized struct that encodes as the jsonrpc version

internal/jsonrpc2/wire_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func TestIDDecode(t *testing.T) {
8181
}
8282
}
8383

84+
func TestErrorEncode(t *testing.T) {
85+
b, err := json.Marshal(jsonrpc2.NewError(0, ""))
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
checkJSON(t, b, []byte(`{
90+
"code": 0,
91+
"message": ""
92+
}`))
93+
}
94+
8495
func TestErrorResponse(t *testing.T) {
8596
// originally reported in #39719, this checks that result is not present if
8697
// it is an error response
@@ -93,8 +104,7 @@ func TestErrorResponse(t *testing.T) {
93104
"jsonrpc":"2.0",
94105
"error":{
95106
"code":0,
96-
"message":"computing fix edits",
97-
"data":null
107+
"message":"computing fix edits"
98108
},
99109
"id":3
100110
}`))

0 commit comments

Comments
 (0)