From 3396a9bedff8c056032eb07f16a4e91a99b15db6 Mon Sep 17 00:00:00 2001 From: Andrey Kolkov Date: Wed, 3 Mar 2021 17:49:56 +0400 Subject: [PATCH] review fixes --- notifier/log_common.go | 10 +++++++--- notifier/notifier_test.go | 4 ++++ senders/telegram/send.go | 22 ++++++++-------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/notifier/log_common.go b/notifier/log_common.go index 8e8a62a81..6dbcb641b 100644 --- a/notifier/log_common.go +++ b/notifier/log_common.go @@ -6,9 +6,13 @@ func getLogWithPackageContext(log *moira.Logger, pkg *NotificationPackage, confi logger := (*log).Clone(). String(moira.LogFieldNameContactID, pkg.Contact.ID). String(moira.LogFieldNameContactType, pkg.Contact.Type). - String(moira.LogFieldNameContactValue, pkg.Contact.Value). - String(moira.LogFieldNameTriggerID, pkg.Trigger.ID). - String(moira.LogFieldNameTriggerName, pkg.Trigger.Name) + String(moira.LogFieldNameContactValue, pkg.Contact.Value) + if pkg.Trigger.ID != "" { // note: test notification without trigger info + logger. + String(moira.LogFieldNameTriggerID, pkg.Trigger.ID). + String(moira.LogFieldNameTriggerName, pkg.Trigger.Name) + } + SetLogLevelByConfig(config.LogContactsToLevel, pkg.Contact.ID, &logger) return logger } diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index 2736c72f4..d2009a655 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -118,6 +118,10 @@ func TestFailSendEvent(t *testing.T) { } func TestNoResendForSendToBrokenContact(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + configureNotifier(t) defer afterTest() diff --git a/senders/telegram/send.go b/senders/telegram/send.go index af2c3bb12..5b73c6799 100644 --- a/senders/telegram/send.go +++ b/senders/telegram/send.go @@ -37,16 +37,10 @@ func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira. sender.logger.Debugf("Calling telegram api with chat_id %s and message body %s", contact.Value, message) chat, err := sender.getChat(contact.Value) if err != nil { - if ok, errorBrokenContact := checkBrokenContactError(sender.logger, err); ok { - return errorBrokenContact - } - return err + return checkBrokenContactError(sender.logger, err) } if err := sender.talk(chat, message, plots, msgType); err != nil { - if ok, errorBrokenContact := checkBrokenContactError(sender.logger, err); ok { - return errorBrokenContact - } - return fmt.Errorf("failed to send message to telegram contact %s: %s. ", contact.Value, err) + return checkBrokenContactError(sender.logger, err) } return nil } @@ -137,22 +131,22 @@ func (sender *Sender) sendAsMessage(chat *telebot.Chat, message string) error { return err } -func checkBrokenContactError(logger moira.Logger, err error) (bool, error) { - logger.Info("Check broken contact") +func checkBrokenContactError(logger moira.Logger, err error) error { + logger.Debug("Check broken contact") if err == nil { - return false, nil + return nil } if e, ok := err.(*telebot.APIError); ok { logger.Debug("It's telebot.APIError from talk(): code = %d, msg = %s, desc = %s", e.Code, e.Message, e.Description) if e.Code == telebot.ErrUnauthorized.Code { // all forbid errors - return true, moira.NewSenderBrokenContactError(err) + return moira.NewSenderBrokenContactError(err) } } if strings.HasPrefix(err.Error(), "failed to get username uuid") { logger.Debug("It's error from getChat(): ", err) - return true, moira.NewSenderBrokenContactError(err) + return moira.NewSenderBrokenContactError(err) } - return false, nil + return err } func prepareAlbum(plots [][]byte, caption string) telebot.Album {