diff --git a/main.go b/main.go index 20052e1..a35bc68 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ var ( version = "dev" ) -func checkMonitors(config *Config) { +func checkMonitors(config *Config) error { for _, monitor := range config.Monitors { if monitor.ShouldCheck() { _, alertNotice := monitor.Check() @@ -43,13 +43,12 @@ func checkMonitors(config *Config) { err, output, ) - // TODO: Maybe return this error instead of panicking here - panic(fmt.Errorf( + return fmt.Errorf( "ERROR: Unsuccessfully triggered alert '%s'. "+ "Crashing to avoid false negatives: %v", alert.Name, err, - )) + ) } } else { // TODO: Maybe panic here. Also, probably validate up front @@ -59,6 +58,8 @@ func checkMonitors(config *Config) { } } } + + return nil } func main() { @@ -81,7 +82,10 @@ func main() { // Start main loop for { - checkMonitors(&config) + err = checkMonitors(&config) + if err != nil { + panic(err) + } sleepTime := time.Duration(config.CheckInterval) * time.Second time.Sleep(sleepTime) diff --git a/main_test.go b/main_test.go index b574dcc..c0ca1ee 100644 --- a/main_test.go +++ b/main_test.go @@ -4,14 +4,14 @@ import "testing" func TestCheckMonitors(t *testing.T) { cases := []struct { - config Config - expectPanic bool - name string + config Config + expectErr bool + name string }{ { - config: Config{}, - expectPanic: false, - name: "Empty", + config: Config{}, + expectErr: false, + name: "Empty", }, { config: Config{ @@ -22,8 +22,8 @@ func TestCheckMonitors(t *testing.T) { }, }, }, - expectPanic: false, - name: "Monitor success, no alerts", + expectErr: false, + name: "Monitor success, no alerts", }, { config: Config{ @@ -41,8 +41,8 @@ func TestCheckMonitors(t *testing.T) { }, }, }, - expectPanic: false, - name: "Monitor failure, no and unknown alerts", + expectErr: false, + name: "Monitor failure, no and unknown alerts", }, { config: Config{ @@ -60,8 +60,8 @@ func TestCheckMonitors(t *testing.T) { }, }, }, - expectPanic: false, - name: "Monitor recovery, no alerts", + expectErr: false, + name: "Monitor recovery, no alerts", }, { config: Config{ @@ -79,8 +79,8 @@ func TestCheckMonitors(t *testing.T) { }, }, }, - expectPanic: false, - name: "Monitor failure, successful alert", + expectErr: false, + name: "Monitor failure, successful alert", }, { config: Config{ @@ -99,24 +99,16 @@ func TestCheckMonitors(t *testing.T) { }, }, }, - expectPanic: true, - name: "Monitor failure, bad alert", + expectErr: true, + name: "Monitor failure, bad alert", }, } for _, c := range cases { - // Create new function so that deferred recovery can run at end of each test case - func() { - // Set up recover to catch panic - defer func() { - if r := recover(); r == nil { - if c.expectPanic { - t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name) - } - } - }() - c.config.Init() - checkMonitors(&c.config) - }() + c.config.Init() + err := checkMonitors(&c.config) + if err == nil && c.expectErr { + t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name) + } } }