From 1f2f313d4775413dcbfc2a81a2a16ffab7e98e06 Mon Sep 17 00:00:00 2001 From: Gennadii Kovalev Date: Wed, 15 Mar 2017 15:30:36 +0200 Subject: [PATCH] encoding/json panic recovery --- send.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/send.go b/send.go index 38c51f9..07e8130 100644 --- a/send.go +++ b/send.go @@ -3,8 +3,9 @@ package gosocketio import ( "encoding/json" "errors" - "time" "github.com/graarh/golang-socketio/protocol" + "log" + "time" ) var ( @@ -16,6 +17,13 @@ var ( Send message packet to socket */ func send(msg *protocol.Message, c *Channel, args interface{}) error { + //preventing json/encoding "index out of range" panic + defer func() { + if r := recover(); r != nil { + log.Println("socket.io send panic: ", r) + } + }() + if args != nil { json, err := json.Marshal(&args) if err != nil { @@ -44,8 +52,8 @@ Create packet based on given data and send it */ func (c *Channel) Emit(method string, args interface{}) error { msg := &protocol.Message{ - Type: protocol.MessageTypeEmit, - Method: method, + Type: protocol.MessageTypeEmit, + Method: method, } return send(msg, c, args) @@ -56,9 +64,9 @@ Create ack packet based on given data and send it and receive response */ func (c *Channel) Ack(method string, args interface{}, timeout time.Duration) (string, error) { msg := &protocol.Message{ - Type: protocol.MessageTypeAckRequest, - AckId: c.ack.getNextId(), - Method: method, + Type: protocol.MessageTypeAckRequest, + AckId: c.ack.getNextId(), + Method: method, } waiter := make(chan string)