mdb: update PubSubs's error logging

This commit is contained in:
Brian Picciano 2018-07-18 23:50:40 +00:00
parent 91f56cb40c
commit 789555eb3a

View File

@ -15,7 +15,7 @@ import (
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
func isErrAlreadyExists(err error) bool { func psIsErrAlreadyExists(err error) bool {
if err == nil { if err == nil {
return false return false
} }
@ -26,8 +26,7 @@ func isErrAlreadyExists(err error) bool {
// Message aliases the type in the official driver // Message aliases the type in the official driver
type Message = pubsub.Message type Message = pubsub.Message
// PubSub holds onto the info needed to communicate with PubSub and wraps a // PubSub is a wrapper around a pubsub client providing more functionality.
// *pubsub.Client
type PubSub struct { type PubSub struct {
*pubsub.Client *pubsub.Client
@ -46,10 +45,7 @@ func CfgPubSub(cfg *mcfg.Cfg) *PubSub {
ps.log.Info("connecting to pubsub") ps.log.Info("connecting to pubsub")
var err error var err error
ps.Client, err = pubsub.NewClient(ctx, ps.gce.Project, ps.gce.ClientOptions()...) ps.Client, err = pubsub.NewClient(ctx, ps.gce.Project, ps.gce.ClientOptions()...)
if err != nil {
return mlog.ErrWithKV(err, &ps) return mlog.ErrWithKV(err, &ps)
}
return nil
}) })
return &ps return &ps
} }
@ -68,22 +64,25 @@ type Topic struct {
// Topic returns, after potentially creating, a topic of the given name // Topic returns, after potentially creating, a topic of the given name
func (ps *PubSub) Topic(ctx context.Context, name string, create bool) (*Topic, error) { func (ps *PubSub) Topic(ctx context.Context, name string, create bool) (*Topic, error) {
kv := mlog.KVerFunc(func() mlog.KV {
return ps.KV().Set("topicName", name)
})
var t *pubsub.Topic var t *pubsub.Topic
var err error var err error
if create { if create {
t, err = ps.Client.CreateTopic(ctx, name) t, err = ps.Client.CreateTopic(ctx, name)
if isErrAlreadyExists(err) { if psIsErrAlreadyExists(err) {
t = ps.Client.Topic(name) t = ps.Client.Topic(name)
} else if err != nil { } else if err != nil {
return nil, err return nil, mlog.ErrWithKV(err, kv)
} }
} else { } else {
t = ps.Client.Topic(name) t = ps.Client.Topic(name)
if exists, err := t.Exists(ctx); err != nil { if exists, err := t.Exists(ctx); err != nil {
panic(err) return nil, mlog.ErrWithKV(err, kv)
return nil, err
} else if !exists { } else if !exists {
return nil, mlog.ErrWithKV(errors.New("topic dne"), ps.KV().Set("topicName", name)) return nil, mlog.ErrWithKV(errors.New("topic dne"), kv)
} }
} }
return &Topic{ return &Topic{
@ -121,23 +120,27 @@ type Subscription struct {
// for the Topic // for the Topic
func (t *Topic) Subscription(ctx context.Context, name string, create bool) (*Subscription, error) { func (t *Topic) Subscription(ctx context.Context, name string, create bool) (*Subscription, error) {
name = t.name + "_" + name name = t.name + "_" + name
kv := mlog.KVerFunc(func() mlog.KV {
return t.KV().Set("subName", name)
})
var s *pubsub.Subscription var s *pubsub.Subscription
var err error var err error
if create { if create {
s, err = t.ps.CreateSubscription(ctx, name, pubsub.SubscriptionConfig{ s, err = t.ps.CreateSubscription(ctx, name, pubsub.SubscriptionConfig{
Topic: t.topic, Topic: t.topic,
}) })
if isErrAlreadyExists(err) { if psIsErrAlreadyExists(err) {
s = t.ps.Subscription(name) s = t.ps.Subscription(name)
} else if err != nil { } else if err != nil {
return nil, err return nil, mlog.ErrWithKV(err, kv)
} }
} else { } else {
s = t.ps.Subscription(name) s = t.ps.Subscription(name)
if exists, err := s.Exists(ctx); err != nil { if exists, err := s.Exists(ctx); err != nil {
return nil, err return nil, mlog.ErrWithKV(err, kv)
} else if !exists { } else if !exists {
return nil, mlog.ErrWithKV(errors.New("sub dne"), t.KV().Set("subName", name)) return nil, mlog.ErrWithKV(errors.New("sub dne"), kv)
} }
} }
return &Subscription{ return &Subscription{