Skip to content

Commit

Permalink
Add support for the ErrorInfo field to the json codec
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleasa committed Apr 4, 2019
1 parent df35be2 commit d8af3b6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
40 changes: 22 additions & 18 deletions rpc/jsoncodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ type inMsgV0 struct {
}

type inMsgV1 struct {
RequestId uint64 `json:"request-id"`
Type string `json:"type"`
Version int `json:"version"`
Id string `json:"id"`
Request string `json:"request"`
Params json.RawMessage `json:"params"`
Error string `json:"error"`
ErrorCode string `json:"error-code"`
Response json.RawMessage `json:"response"`
RequestId uint64 `json:"request-id"`
Type string `json:"type"`
Version int `json:"version"`
Id string `json:"id"`
Request string `json:"request"`
Params json.RawMessage `json:"params"`
Error string `json:"error"`
ErrorCode string `json:"error-code"`
ErrorInfo map[string]interface{} `json:"error-info"`
Response json.RawMessage `json:"response"`
}

// outMsg holds an outgoing message.
Expand All @@ -88,15 +89,16 @@ type outMsgV0 struct {
}

type outMsgV1 struct {
RequestId uint64 `json:"request-id,omitempty"`
Type string `json:"type,omitempty"`
Version int `json:"version,omitempty"`
Id string `json:"id,omitempty"`
Request string `json:"request,omitempty"`
Params interface{} `json:"params,omitempty"`
Error string `json:"error,omitempty"`
ErrorCode string `json:"error-code,omitempty"`
Response interface{} `json:"response,omitempty"`
RequestId uint64 `json:"request-id,omitempty"`
Type string `json:"type,omitempty"`
Version int `json:"version,omitempty"`
Id string `json:"id,omitempty"`
Request string `json:"request,omitempty"`
Params interface{} `json:"params,omitempty"`
Error string `json:"error,omitempty"`
ErrorCode string `json:"error-code,omitempty"`
ErrorInfo map[string]interface{} `json:"error-info,omitempty"`
Response interface{} `json:"response,omitempty"`
}

func (c *Codec) Close() error {
Expand Down Expand Up @@ -139,6 +141,7 @@ func (c *Codec) ReadHeader(hdr *rpc.Header) error {
}
hdr.Error = c.msg.Error
hdr.ErrorCode = c.msg.ErrorCode
hdr.ErrorInfo = c.msg.ErrorInfo
hdr.Version = version
return nil
}
Expand Down Expand Up @@ -272,6 +275,7 @@ func newOutMsgV1(hdr *rpc.Header, body interface{}) outMsgV1 {
Request: hdr.Request.Action,
Error: hdr.Error,
ErrorCode: hdr.ErrorCode,
ErrorInfo: hdr.ErrorInfo,
}
if hdr.IsRequest() {
result.Params = body
Expand Down
35 changes: 35 additions & 0 deletions rpc/jsoncodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ func (*suite) TestRead(c *gc.C) {
Version: 1,
},
expectBody: new(map[string]interface{}),
}, {
msg: `{"request-id": 2, "error": "an error", "error-code": "a code", "error-info": {"foo": "bar", "baz": true}}`,
expectHdr: rpc.Header{
RequestId: 2,
Error: "an error",
ErrorCode: "a code",
ErrorInfo: map[string]interface{}{
"foo": "bar",
"baz": true,
},
Version: 1,
},
expectBody: new(map[string]interface{}),
}, {
msg: `{"request-id": 3, "response": {"X": "result"}}`,
expectHdr: rpc.Header{
Expand Down Expand Up @@ -178,6 +191,16 @@ func (*suite) TestWrite(c *gc.C) {
ErrorCode: "a code",
},
expect: `{"RequestId": 2, "Error": "an error", "ErrorCode": "a code"}`,
}, {
hdr: &rpc.Header{
RequestId: 2,
Error: "an error",
ErrorCode: "a code",
ErrorInfo: map[string]interface{}{
"ignored": "for version0",
},
},
expect: `{"RequestId": 2, "Error": "an error", "ErrorCode": "a code"}`,
}, {
hdr: &rpc.Header{
RequestId: 3,
Expand Down Expand Up @@ -216,6 +239,18 @@ func (*suite) TestWrite(c *gc.C) {
Version: 1,
},
expect: `{"request-id": 2, "error": "an error", "error-code": "a code"}`,
}, {
hdr: &rpc.Header{
RequestId: 2,
Error: "an error",
ErrorCode: "a code",
ErrorInfo: map[string]interface{}{
"foo": "bar",
"baz": true,
},
Version: 1,
},
expect: `{"request-id": 2, "error": "an error", "error-code": "a code", "error-info": {"foo": "bar", "baz": true}}`,
}, {
hdr: &rpc.Header{
RequestId: 3,
Expand Down

0 comments on commit d8af3b6

Please sign in to comment.