Respond with error type

This commit is contained in:
Asim Aslam 2019-01-13 12:15:35 +00:00
parent 01f6683035
commit e1bc240a14
4 changed files with 12 additions and 9 deletions

View File

@ -31,7 +31,7 @@ func (j *jsonCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type { switch m.Type {
case codec.Request: case codec.Request:
return j.c.Write(m, b) return j.c.Write(m, b)
case codec.Response: case codec.Response, codec.Error:
return j.s.Write(m, b) return j.s.Write(m, b)
case codec.Publication: case codec.Publication:
data, err := json.Marshal(b) data, err := json.Marshal(b)

View File

@ -70,7 +70,7 @@ func (c *protoCodec) Write(m *codec.Message, b interface{}) error {
return err return err
} }
} }
case codec.Response: case codec.Response, codec.Error:
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
rtmp := &Response{ServiceMethod: &m.Endpoint, Seq: id(m.Id), Error: &m.Error} rtmp := &Response{ServiceMethod: &m.Endpoint, Seq: id(m.Id), Error: &m.Error}

View File

@ -110,14 +110,14 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
return return
} }
codec := newRpcCodec(&msg, sock, cf) rcodec := newRpcCodec(&msg, sock, cf)
// internal request // internal request
request := &rpcRequest{ request := &rpcRequest{
service: msg.Header["X-Micro-Service"], service: msg.Header["X-Micro-Service"],
endpoint: msg.Header["X-Micro-Endpoint"], endpoint: msg.Header["X-Micro-Endpoint"],
contentType: ct, contentType: ct,
codec: codec, codec: rcodec,
header: msg.Header, header: msg.Header,
body: msg.Body, body: msg.Body,
socket: sock, socket: sock,
@ -128,7 +128,7 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
response := &rpcResponse{ response := &rpcResponse{
header: make(map[string]string), header: make(map[string]string),
socket: sock, socket: sock,
codec: codec, codec: rcodec,
} }
// set router // set router
@ -150,8 +150,14 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
// TODO: handle error better // TODO: handle error better
if err := handler(ctx, request, response); err != nil { if err := handler(ctx, request, response); err != nil {
// write an error response
rcodec.Write(&codec.Message{
Header: msg.Header,
Error: err.Error(),
Type: codec.Error,
}, nil)
s.wg.Done() s.wg.Done()
log.Logf("Unexpected error serving request, closing socket: %v", err)
return return
} }

View File

@ -245,9 +245,6 @@ func (h *httpTransportSocket) Recv(m *Message) error {
} }
} }
// set path
m.Header[":path"] = h.r.URL.Path
// return early early // return early early
return nil return nil
} }