From bf12f5b57cf74728ba6e972cf26b8e425904f084 Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 3 Sep 2020 11:24:29 +0800 Subject: [PATCH] feat(notify): break down error from UseridByMobile --- pkg/notify/interface.go | 7 +++++++ pkg/notify/rpc/send.go | 26 ++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/notify/interface.go b/pkg/notify/interface.go index 5f24369a28..49c382e28b 100644 --- a/pkg/notify/interface.go +++ b/pkg/notify/interface.go @@ -17,6 +17,8 @@ package notify import ( "context" + "yunion.io/x/pkg/errors" + "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/notify/rpc/apis" ) @@ -42,3 +44,8 @@ type ITemplateStore interface { } type SConfig map[string]string + +var ( + ErrNoSuchMobile = errors.Error("no such mobile") + ErrIncompleteConfig = errors.Error("incomplete config") +) diff --git a/pkg/notify/rpc/send.go b/pkg/notify/rpc/send.go index 6d5240e4d1..f7c9ce52ae 100644 --- a/pkg/notify/rpc/send.go +++ b/pkg/notify/rpc/send.go @@ -124,7 +124,11 @@ func (self *SRpcService) Send(ctx context.Context, contactType, contact, topic, _, err = self.execute(ctx, f, contactType) if err != nil { - return errors.Wrapf(err, "contactType '%s'", contactType) + s, ok := status.FromError(err) + if !ok { + return err + } + return errors.Error(s.Message()) } return nil } @@ -149,7 +153,11 @@ func (self *SRpcService) BatchSend(ctx context.Context, contacts []string, conta ret, err := self.execute(ctx, f, contactType) if err != nil { - return nil, errors.Wrapf(err, "contactType '%s'", contactType) + s, ok := status.FromError(err) + if !ok { + return nil, err + } + return nil, errors.Error(s.Message()) } reply := ret.(*apis.BatchSendReply) return reply.FailedRecords, nil @@ -178,6 +186,16 @@ func (self *SRpcService) ContactByMobile(ctx context.Context, mobile, serviceNam if err != nil { return "", err } + s, ok := status.FromError(err) + if !ok { + return "", err + } + if s.Code() == codes.NotFound { + return "", errors.Wrap(notifyv2.ErrNoSuchMobile, s.Message()) + } + if s.Code() == codes.PermissionDenied { + return "", errors.Wrap(notifyv2.ErrIncompleteConfig, s.Message()) + } reply := ret.(*apis.UseridByMobileReply) return reply.Userid, nil @@ -212,7 +230,7 @@ func (self *SRpcService) execute(ctx context.Context, f func(client *apis.SendNo } if st.Message() != ErrSendServiceNotInit.Error() { - return nil, errors.Error(st.Message()) + return nil, err } // if NOINIT, try to restart server and send again @@ -230,7 +248,7 @@ func (self *SRpcService) execute(ctx context.Context, f func(client *apis.SendNo return nil, errors.Wrap(ErrSendServiceNotFound, serviceName) } - return nil, errors.Error(st.Message()) + return nil, err } } return ret, nil