simplify stan handler error handling

This commit is contained in:
Asim Aslam 2019-01-28 08:02:42 +00:00 committed by Vasiliy Tolstov
parent 6c04d1a29b
commit 6eb8ae50d5

14
stan.go
View File

@ -224,14 +224,16 @@ func (n *stanBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
fn := func(msg *stan.Msg) {
var m broker.Message
var err error
if err = n.opts.Codec.Unmarshal(msg.Data, &m); err != nil {
// unmarshal message
if err := n.opts.Codec.Unmarshal(msg.Data, &m); err != nil {
return
}
err = handler(&publication{m: &m, msg: msg, t: msg.Subject})
if err == nil && successAutoAck && !opt.AutoAck {
msg.Ack()
} else if err != nil && opt.AutoAck {
// execute the handler
err := handler(&publication{m: &m, msg: msg, t: msg.Subject})
// if there's no error and success auto ack is enabled ack it
if err == nil && successAutoAck {
msg.Ack()
}
}