diff options
author | 2018-09-12 17:38:36 +0200 | |
---|---|---|
committer | 2018-09-12 17:38:36 +0200 | |
commit | 0f731471ab258d9e01658f0ab70f8317fd0fb89c (patch) | |
tree | daee229ec6fe19119f9284c119dc53b298fd7b5c /codec/msg_codec.go | |
parent | 6f6d2433ae72550cb4b603376ae8e6185256affe (diff) |
Recover possible panic in EncodeMsg and improve debug logs
Change-Id: I771c171ae30a957f4436e7f4ba834d8a38d02f80
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'codec/msg_codec.go')
-rw-r--r-- | codec/msg_codec.go | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/codec/msg_codec.go b/codec/msg_codec.go index 9d3f614..67628a4 100644 --- a/codec/msg_codec.go +++ b/codec/msg_codec.go @@ -53,24 +53,32 @@ type VppOtherHeader struct { } // EncodeMsg encodes provided `Message` structure into its binary-encoded data representation. -func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) ([]byte, error) { +func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) (data []byte, err error) { if msg == nil { return nil, errors.New("nil message passed in") } + // try to recover panic which might possibly occur in struc.Pack call + defer func() { + if r := recover(); r != nil { + var ok bool + if err, ok = r.(error); !ok { + err = fmt.Errorf("%v", r) + } + err = fmt.Errorf("panic occurred: %v", err) + } + }() + var header interface{} // encode message header switch msg.GetMessageType() { case api.RequestMessage: header = &VppRequestHeader{VlMsgID: msgID} - case api.ReplyMessage: header = &VppReplyHeader{VlMsgID: msgID} - case api.EventMessage: header = &VppEventHeader{VlMsgID: msgID} - default: header = &VppOtherHeader{VlMsgID: msgID} } @@ -79,13 +87,13 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) ([]byte, error) { // encode message header if err := struc.Pack(buf, header); err != nil { - return nil, fmt.Errorf("unable to encode message header: %v, error %v", header, err) + return nil, fmt.Errorf("failed to encode message header: %+v, error: %v", header, err) } // encode message content if reflect.TypeOf(msg).Elem().NumField() > 0 { if err := struc.Pack(buf, msg); err != nil { - return nil, fmt.Errorf("unable to encode message data: %v, error %v", header, err) + return nil, fmt.Errorf("failed to encode message data: %+v, error: %v", data, err) } } @@ -104,13 +112,10 @@ func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error { switch msg.GetMessageType() { case api.RequestMessage: header = new(VppRequestHeader) - case api.ReplyMessage: header = new(VppReplyHeader) - case api.EventMessage: header = new(VppEventHeader) - default: header = new(VppOtherHeader) } @@ -119,12 +124,12 @@ func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error { // decode message header if err := struc.Unpack(buf, header); err != nil { - return fmt.Errorf("unable to decode message header: %+v, error %v", data, err) + return fmt.Errorf("failed to decode message header: %+v, error: %v", header, err) } // decode message content if err := struc.Unpack(buf, msg); err != nil { - return fmt.Errorf("unable to decode message data: %+v, error %v", data, err) + return fmt.Errorf("failed to decode message data: %+v, error: %v", data, err) } return nil |