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 {
case codec.Request:
return j.c.Write(m, b)
case codec.Response:
case codec.Response, codec.Error:
return j.s.Write(m, b)
case codec.Publication:
data, err := json.Marshal(b)

View File

@ -70,7 +70,7 @@ func (c *protoCodec) Write(m *codec.Message, b interface{}) error {
return err
}
}
case codec.Response:
case codec.Response, codec.Error:
c.Lock()
defer c.Unlock()
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
}
codec := newRpcCodec(&msg, sock, cf)
rcodec := newRpcCodec(&msg, sock, cf)
// internal request
request := &rpcRequest{
service: msg.Header["X-Micro-Service"],
endpoint: msg.Header["X-Micro-Endpoint"],
contentType: ct,
codec: codec,
codec: rcodec,
header: msg.Header,
body: msg.Body,
socket: sock,
@ -128,7 +128,7 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
response := &rpcResponse{
header: make(map[string]string),
socket: sock,
codec: codec,
codec: rcodec,
}
// set router
@ -150,8 +150,14 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
// TODO: handle error better
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()
log.Logf("Unexpected error serving request, closing socket: %v", err)
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 nil
}