Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Kolkov committed Mar 5, 2021
1 parent c5d0b3f commit 3396a9b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 7 additions & 3 deletions notifier/log_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
22 changes: 8 additions & 14 deletions senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3396a9b

Please sign in to comment.