diff --git a/protocol/codec.go b/protocol/codec.go index b47133c..cad843a 100644 --- a/protocol/codec.go +++ b/protocol/codec.go @@ -10,7 +10,7 @@ import ( // Codec creates a CodecRequest to process each request. type Codec interface { - NewRequest(rc io.Reader) CodecRequest + NewRequest(rc io.Reader) (CodecRequest, error) } // CodecRequest decodes a request and encodes a response using a specific diff --git a/protocol/json/server.go b/protocol/json/server.go index 32c8546..cd80a00 100644 --- a/protocol/json/server.go +++ b/protocol/json/server.go @@ -72,7 +72,7 @@ type Codec struct { } // NewRequest returns a CodecRequest. -func (c *Codec) NewRequest(r io.Reader) protocol.CodecRequest { +func (c *Codec) NewRequest(r io.Reader) (protocol.CodecRequest, error) { return newCodecRequest(r, c.encSel.Select(r)) } @@ -81,12 +81,13 @@ func (c *Codec) NewRequest(r io.Reader) protocol.CodecRequest { // ---------------------------------------------------------------------------- // newCodecRequest returns a new CodecRequest. -func newCodecRequest(r io.Reader, encoder encode.Encoder) protocol.CodecRequest { +func newCodecRequest(r io.Reader, encoder encode.Encoder) (protocol.CodecRequest, error) { // Decode the request body and check if RPC method is valid. req := new(serverRequest) err := json.NewDecoder(r).Decode(req) if err == io.ErrUnexpectedEOF || err == io.EOF { log.Printf("NewRequest err: %v", err) + return nil, err } if err != nil { err = &Error{ @@ -103,7 +104,7 @@ func newCodecRequest(r io.Reader, encoder encode.Encoder) protocol.CodecRequest } } - return &CodecRequest{request: req, err: err, encoder: encoder} + return &CodecRequest{request: req, err: err, encoder: encoder}, nil } // CodecRequest decodes and encodes a single request. diff --git a/registry.go b/registry.go index 555df63..25ce4b4 100644 --- a/registry.go +++ b/registry.go @@ -101,7 +101,10 @@ func (rr *rpcRegistry) Invoke(contentType string, r io.Reader, w io.Writer, befo log.Print("codec.NewRequest") // Create a new codec request. - codecReq := codec.NewRequest(r) + codecReq, errNew := codec.NewRequest(r) + if nil != errNew { + return errNew + } // Get service method to be called. log.Printf("codecReq.Method: %v", codecReq) method, errMethod := codecReq.Method()