From 5aeb28dfee621f449560d3f2a6a8f25a92421b5a Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 7 Jan 2019 09:11:36 +0000 Subject: [PATCH] Add error header --- client/rpc_codec.go | 18 ++++++++++++++++++ server/rpc_codec.go | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/client/rpc_codec.go b/client/rpc_codec.go index 67959b6a..1a341a7c 100644 --- a/client/rpc_codec.go +++ b/client/rpc_codec.go @@ -3,6 +3,8 @@ package client import ( "bytes" errs "errors" + "fmt" + "strconv" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/json" @@ -112,6 +114,7 @@ func (c *rpcCodec) WriteRequest(req *request, body interface{}) error { Method: req.ServiceMethod, Type: codec.Request, Header: map[string]string{ + "X-Micro-Id": fmt.Sprintf("%d", req.Seq), "X-Micro-Service": req.Service, "X-Micro-Method": req.ServiceMethod, }, @@ -147,6 +150,21 @@ func (c *rpcCodec) ReadResponse(r *response, b interface{}) error { r.Seq = me.Id r.Error = me.Error + // check error in header + if len(me.Error) == 0 { + r.Error = me.Header["X-Micro-Error"] + } + + // check method in header + if len(me.Method) == 0 { + r.ServiceMethod = me.Header["X-Micro-Method"] + } + + if me.Id == 0 && len(me.Header["X-Micro-Id"]) > 0 { + id, _ := strconv.ParseInt(me.Header["X-Micro-Id"], 10, 64) + r.Seq = uint64(id) + } + if err != nil { return errors.InternalServerError("go.micro.client.codec", err.Error()) } diff --git a/server/rpc_codec.go b/server/rpc_codec.go index f19b2782..6afc4e4b 100644 --- a/server/rpc_codec.go +++ b/server/rpc_codec.go @@ -2,6 +2,8 @@ package server import ( "bytes" + "fmt" + "strconv" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/grpc" @@ -87,9 +89,17 @@ func (c *rpcCodec) ReadRequestHeader(r *request, first bool) error { m.Target = m.Header["X-Micro-Service"] m.Method = m.Header["X-Micro-Method"] + // set id + if len(m.Header["X-Micro-Id"]) > 0 { + id, _ := strconv.ParseInt(m.Header["X-Micro-Id"], 10, 64) + m.Id = uint64(id) + } + + // read header via codec err := c.codec.ReadHeader(&m, codec.Request) r.ServiceMethod = m.Method r.Seq = m.Id + return err } @@ -104,11 +114,16 @@ func (c *rpcCodec) WriteResponse(r *response, body interface{}, last bool) error Id: r.Seq, Error: r.Error, Type: codec.Response, - Header: map[string]string{}, + Header: map[string]string{ + "X-Micro-Id": fmt.Sprintf("%d", r.Seq), + "X-Micro-Method": r.ServiceMethod, + "X-Micro-Error": r.Error, + }, } if err := c.codec.Write(m, body); err != nil { c.buf.wbuf.Reset() m.Error = errors.Wrapf(err, "Unable to encode body").Error() + m.Header["X-Micro-Error"] = m.Error if err := c.codec.Write(m, nil); err != nil { return err }