type addResp struct {
Id interface{} "id"
Result Reply "result"
- Error string "error"
+ Error interface{} "error"
}
cli, srv := net.Pipe()
if err != nil {
t.Fatalf("Decode: %s", err)
}
- if resp.Error != "" {
+ if resp.Error != nil {
t.Fatalf("resp.Error: %s", resp.Error)
}
if resp.Id.(string) != string(i) {
t.Fatalf("resp: bad result: %d+%d=%d", i, i+1, resp.Result.C)
}
}
+
+ fmt.Fprintf(cli, "{}\n")
+ var resp addResp
+ if err := dec.Decode(&resp); err != nil {
+ t.Fatalf("Decode after empty: %s", err)
+ }
+ if resp.Error == nil {
+ t.Fatalf("Expected error, got nil")
+ }
}
func TestClient(t *testing.T) {
package jsonrpc
import (
+ "fmt"
"io"
"json"
"net"
type clientResponse struct {
Id uint64 "id"
Result *json.RawMessage "result"
- Error string "error"
+ Error interface{} "error"
}
func (r *clientResponse) reset() {
r.Id = 0
r.Result = nil
- r.Error = ""
+ r.Error = nil
}
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) os.Error {
c.pending[c.resp.Id] = "", false
c.mutex.Unlock()
+ r.Error = ""
r.Seq = c.resp.Id
- r.Error = c.resp.Error
+ if c.resp.Error != nil {
+ x, ok := c.resp.Error.(string)
+ if !ok {
+ return os.NewError(fmt.Sprintf("invalid error %v", c.resp.Error))
+ }
+ if x == "" {
+ x = "unspecified error"
+ }
+ r.Error = x
+ }
return nil
}
type serverResponse struct {
Id *json.RawMessage "id"
Result interface{} "result"
- Error string "error"
+ Error interface{} "error"
}
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) os.Error {
return json.Unmarshal(*c.req.Params, ¶ms)
}
+var null = json.RawMessage([]byte("null"))
+
func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) os.Error {
var resp serverResponse
c.mutex.Lock()
c.pending[r.Seq] = nil, false
c.mutex.Unlock()
+ if b == nil {
+ // Invalid request so no id. Use JSON null.
+ b = &null
+ }
resp.Id = b
resp.Result = x
- resp.Error = r.Error
+ if r.Error == "" {
+ resp.Error = nil
+ } else {
+ resp.Error = r.Error
+ }
return c.enc.Encode(resp)
}