Skip to content

Commit

Permalink
feat: crash on reporter init error
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 19, 2023
1 parent b27cf2b commit 212a9f3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/reporters/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type Reporter interface {
Init()
Init() error
Enabled() bool
SendReport(report Report) error
Name() string
Expand Down
8 changes: 5 additions & 3 deletions pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -74,6 +74,8 @@ func (reporter *Reporter) Init() {

reporter.TelegramBot = bot
go reporter.TelegramBot.Start()

return nil
}

func (reporter *Reporter) Enabled() bool {
Expand Down

0 comments on commit 212a9f3

Please sign in to comment.