return error instead of panic

This commit is contained in:
Sergei Vizel 2018-09-20 08:48:33 +03:00
parent f9d710b126
commit c45cd2a7b3

View File

@ -62,17 +62,17 @@ func (w *Websocket) Init(endpoint string, options *transport.Options) error {
return nil return nil
} }
func (w *Websocket) readWorker() { func (w *Websocket) readWorker() error {
for { for {
select { select {
case err := <-w.stopCh: case err := <-w.stopCh:
panic(err) return err
default: default:
} }
var payload []message.Message var payload []message.Message
err := w.conn.ReadJSON(&payload) err := w.conn.ReadJSON(&payload)
if err != nil { if err != nil {
panic(err) return err
} }
//dispatch //dispatch
msg := &payload[0] msg := &payload[0]
@ -93,14 +93,14 @@ func (w *Websocket) readWorker() {
Id: w.nextMsgID(), Id: w.nextMsgID(),
} }
if err = w.sendMessage(&m); err != nil { if err = w.sendMessage(&m); err != nil {
panic(err) return err
} }
case transport.MetaSubscribe: case transport.MetaSubscribe:
//handle MetaSubscribe resp //handle MetaSubscribe resp
w.subscriptionsMu.Lock() w.subscriptionsMu.Lock()
subscriptions, ok := w.subscriptions[msg.Subscription] subscriptions, ok := w.subscriptions[msg.Subscription]
if !ok { if !ok {
panic("BUG: subscription not registered `" + msg.Subscription + "`") return fmt.Errorf("BUG: subscription not registered `%s`", msg.Subscription)
} }
if !msg.Successful { if !msg.Successful {
if msg.GetError() == nil { if msg.GetError() == nil {
@ -251,12 +251,8 @@ func (w *Websocket) Connect() error {
} }
go func () { go func () {
defer func() { err := w.readWorker()
if r := recover(); r != nil { w.onError(err)
w.onError(fmt.Errorf("%v", r))
}
}()
w.readWorker()
}() }()
return w.sendMessage(&m) return w.sendMessage(&m)