diff --git a/senders/telegram/init.go b/senders/telegram/init.go index 4659c1f69..aabd2a4fd 100644 --- a/senders/telegram/init.go +++ b/senders/telegram/init.go @@ -80,8 +80,9 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca sender.logger = logger sender.bot, err = telebot.NewBot(telebot.Settings{ - Token: cfg.APIToken, - Poller: &telebot.LongPoller{Timeout: pollerTimeout}, + Token: cfg.APIToken, + Poller: &telebot.LongPoller{Timeout: pollerTimeout}, + OnError: sender.customOnErrorFunc, }) if err != nil { return sender.removeTokenFromError(err) @@ -123,3 +124,13 @@ func (sender *Sender) runTelebot(contactType string) { func telegramLockKey(contactType string) string { return telegramLockPrefix + contactType } + +const errorInsideTelebotMsg = "Error inside telebot" + +func (sender *Sender) customOnErrorFunc(err error, _ telebot.Context) { + err = sender.removeTokenFromError(err) + + sender.logger.Warning(). + Error(err). + Msg(errorInsideTelebotMsg) +} diff --git a/senders/telegram/init_test.go b/senders/telegram/init_test.go index f55cdfcd0..9179cb920 100644 --- a/senders/telegram/init_test.go +++ b/senders/telegram/init_test.go @@ -1,10 +1,15 @@ package telegram import ( + "errors" "fmt" + "strings" "testing" "time" + mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" + "go.uber.org/mock/gomock" + logging "github.com/moira-alert/moira/logging/zerolog_adapter" . "github.com/smartystreets/goconvey/convey" ) @@ -31,3 +36,26 @@ func TestInit(t *testing.T) { }) }) } + +func Test_customOnErrorFunc(t *testing.T) { + Convey("test customOnErrorFunc hides credential and logs", t, func() { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + logger := mock_moira_alert.NewMockLogger(mockCtrl) + eventsBuilder := mock_moira_alert.NewMockEventBuilder(mockCtrl) + + sender := Sender{ + logger: logger, + apiToken: "1111111111:SecretTokenabc_987654321hellokonturmoira", + } + + err := fmt.Errorf("https://some.api.of.telegram/bot%s/update failed to update", sender.apiToken) + + logger.EXPECT().Warning().Return(eventsBuilder).AnyTimes() + eventsBuilder.EXPECT().Error(errors.New(strings.ReplaceAll(err.Error(), sender.apiToken, hidden))).Return(eventsBuilder) + eventsBuilder.EXPECT().Msg(errorInsideTelebotMsg) + + sender.customOnErrorFunc(err, nil) + }) +}