From 212a9f398adba27a6d2957a715ceffa04efeeba7 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Thu, 19 Oct 2023 22:44:49 +0300 Subject: [PATCH] feat: crash on reporter init error --- pkg/app.go | 4 +++- pkg/reporters/pagerduty/pagerduty.go | 3 ++- pkg/reporters/reporter.go | 2 +- pkg/reporters/telegram/telegram.go | 8 +++++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/app.go b/pkg/app.go index 04ca39a..de152b5 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -60,7 +60,9 @@ func (a *App) Start() { a.MutesManager.Load() for _, reporter := range a.Reporters { - reporter.Init() + if err := reporter.Init(); err != nil { + a.Logger.Fatal().Err(err).Str("name", reporter.Name()).Msg("Error initializing reporter") + } if reporter.Enabled() { a.Logger.Info().Str("name", reporter.Name()).Msg("Init reporter") } diff --git a/pkg/reporters/pagerduty/pagerduty.go b/pkg/reporters/pagerduty/pagerduty.go index 240af53..3fc9c59 100644 --- a/pkg/reporters/pagerduty/pagerduty.go +++ b/pkg/reporters/pagerduty/pagerduty.go @@ -120,7 +120,8 @@ func NewPagerDutyReporter(config config.PagerDutyConfig, logger *zerolog.Logger) } } -func (r Reporter) Init() { +func (r Reporter) Init() error { + return nil } func (r Reporter) Enabled() bool { diff --git a/pkg/reporters/reporter.go b/pkg/reporters/reporter.go index 1f8c88c..6c87b78 100644 --- a/pkg/reporters/reporter.go +++ b/pkg/reporters/reporter.go @@ -5,7 +5,7 @@ import ( ) type Reporter interface { - Init() + Init() error Enabled() bool SendReport(report Report) error Name() string diff --git a/pkg/reporters/telegram/telegram.go b/pkg/reporters/telegram/telegram.go index ae0e621..2efe63e 100644 --- a/pkg/reporters/telegram/telegram.go +++ b/pkg/reporters/telegram/telegram.go @@ -50,10 +50,10 @@ func NewTelegramReporter( } } -func (reporter *Reporter) Init() { +func (reporter *Reporter) Init() error { if reporter.TelegramToken == "" || reporter.TelegramChat == 0 { reporter.Logger.Debug().Msg("Telegram credentials not set, not creating Telegram reporter.") - return + return nil } bot, err := tele.NewBot(tele.Settings{ @@ -63,7 +63,7 @@ func (reporter *Reporter) Init() { if err != nil { reporter.Logger.Warn().Err(err).Msg("Could not create Telegram bot") - return + return err } bot.Handle("/start", reporter.HandleHelp) @@ -74,6 +74,8 @@ func (reporter *Reporter) Init() { reporter.TelegramBot = bot go reporter.TelegramBot.Start() + + return nil } func (reporter *Reporter) Enabled() bool {