1
0
mirror of https://github.com/golang/go synced 2024-11-12 07:40:23 -07:00

jsonrpc: use error: null for success, not error: ""

handle missing id in server.

Fixes #1017.
Fixes #1018.

R=r
CC=golang-dev
https://golang.org/cl/1986044
This commit is contained in:
Russ Cox 2010-08-17 18:55:42 -04:00
parent 340f28deab
commit 8dc4c0b45f
3 changed files with 37 additions and 7 deletions

View File

@ -53,7 +53,7 @@ func TestServer(t *testing.T) {
type addResp struct { type addResp struct {
Id interface{} "id" Id interface{} "id"
Result Reply "result" Result Reply "result"
Error string "error" Error interface{} "error"
} }
cli, srv := net.Pipe() cli, srv := net.Pipe()
@ -69,7 +69,7 @@ func TestServer(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Decode: %s", err) t.Fatalf("Decode: %s", err)
} }
if resp.Error != "" { if resp.Error != nil {
t.Fatalf("resp.Error: %s", resp.Error) t.Fatalf("resp.Error: %s", resp.Error)
} }
if resp.Id.(string) != string(i) { if resp.Id.(string) != string(i) {
@ -79,6 +79,15 @@ func TestServer(t *testing.T) {
t.Fatalf("resp: bad result: %d+%d=%d", i, i+1, resp.Result.C) 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) { func TestClient(t *testing.T) {

View File

@ -7,6 +7,7 @@
package jsonrpc package jsonrpc
import ( import (
"fmt"
"io" "io"
"json" "json"
"net" "net"
@ -61,13 +62,13 @@ func (c *clientCodec) WriteRequest(r *rpc.Request, param interface{}) os.Error {
type clientResponse struct { type clientResponse struct {
Id uint64 "id" Id uint64 "id"
Result *json.RawMessage "result" Result *json.RawMessage "result"
Error string "error" Error interface{} "error"
} }
func (r *clientResponse) reset() { func (r *clientResponse) reset() {
r.Id = 0 r.Id = 0
r.Result = nil r.Result = nil
r.Error = "" r.Error = nil
} }
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) os.Error { func (c *clientCodec) ReadResponseHeader(r *rpc.Response) os.Error {
@ -81,8 +82,18 @@ func (c *clientCodec) ReadResponseHeader(r *rpc.Response) os.Error {
c.pending[c.resp.Id] = "", false c.pending[c.resp.Id] = "", false
c.mutex.Unlock() c.mutex.Unlock()
r.Error = ""
r.Seq = c.resp.Id 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 return nil
} }

View File

@ -61,7 +61,7 @@ func (r *serverRequest) reset() {
type serverResponse struct { type serverResponse struct {
Id *json.RawMessage "id" Id *json.RawMessage "id"
Result interface{} "result" Result interface{} "result"
Error string "error" Error interface{} "error"
} }
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) os.Error { func (c *serverCodec) ReadRequestHeader(r *rpc.Request) os.Error {
@ -94,6 +94,8 @@ func (c *serverCodec) ReadRequestBody(x interface{}) os.Error {
return json.Unmarshal(*c.req.Params, &params) return json.Unmarshal(*c.req.Params, &params)
} }
var null = json.RawMessage([]byte("null"))
func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) os.Error { func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) os.Error {
var resp serverResponse var resp serverResponse
c.mutex.Lock() c.mutex.Lock()
@ -105,9 +107,17 @@ func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) os.Error {
c.pending[r.Seq] = nil, false c.pending[r.Seq] = nil, false
c.mutex.Unlock() c.mutex.Unlock()
if b == nil {
// Invalid request so no id. Use JSON null.
b = &null
}
resp.Id = b resp.Id = b
resp.Result = x resp.Result = x
if r.Error == "" {
resp.Error = nil
} else {
resp.Error = r.Error resp.Error = r.Error
}
return c.enc.Encode(resp) return c.enc.Encode(resp)
} }