Move panics up to main function

This commit is contained in:
IamTheFij 2019-10-04 16:17:36 -07:00
parent 1ad5bcef14
commit 6b9edf75d7
2 changed files with 30 additions and 34 deletions

14
main.go
View File

@ -15,7 +15,7 @@ var (
version = "dev" version = "dev"
) )
func checkMonitors(config *Config) { func checkMonitors(config *Config) error {
for _, monitor := range config.Monitors { for _, monitor := range config.Monitors {
if monitor.ShouldCheck() { if monitor.ShouldCheck() {
_, alertNotice := monitor.Check() _, alertNotice := monitor.Check()
@ -43,13 +43,12 @@ func checkMonitors(config *Config) {
err, err,
output, output,
) )
// TODO: Maybe return this error instead of panicking here return fmt.Errorf(
panic(fmt.Errorf(
"ERROR: Unsuccessfully triggered alert '%s'. "+ "ERROR: Unsuccessfully triggered alert '%s'. "+
"Crashing to avoid false negatives: %v", "Crashing to avoid false negatives: %v",
alert.Name, alert.Name,
err, err,
)) )
} }
} else { } else {
// TODO: Maybe panic here. Also, probably validate up front // TODO: Maybe panic here. Also, probably validate up front
@ -59,6 +58,8 @@ func checkMonitors(config *Config) {
} }
} }
} }
return nil
} }
func main() { func main() {
@ -81,7 +82,10 @@ func main() {
// Start main loop // Start main loop
for { for {
checkMonitors(&config) err = checkMonitors(&config)
if err != nil {
panic(err)
}
sleepTime := time.Duration(config.CheckInterval) * time.Second sleepTime := time.Duration(config.CheckInterval) * time.Second
time.Sleep(sleepTime) time.Sleep(sleepTime)

View File

@ -4,14 +4,14 @@ import "testing"
func TestCheckMonitors(t *testing.T) { func TestCheckMonitors(t *testing.T) {
cases := []struct { cases := []struct {
config Config config Config
expectPanic bool expectErr bool
name string name string
}{ }{
{ {
config: Config{}, config: Config{},
expectPanic: false, expectErr: false,
name: "Empty", name: "Empty",
}, },
{ {
config: Config{ config: Config{
@ -22,8 +22,8 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectPanic: false, expectErr: false,
name: "Monitor success, no alerts", name: "Monitor success, no alerts",
}, },
{ {
config: Config{ config: Config{
@ -41,8 +41,8 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectPanic: false, expectErr: false,
name: "Monitor failure, no and unknown alerts", name: "Monitor failure, no and unknown alerts",
}, },
{ {
config: Config{ config: Config{
@ -60,8 +60,8 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectPanic: false, expectErr: false,
name: "Monitor recovery, no alerts", name: "Monitor recovery, no alerts",
}, },
{ {
config: Config{ config: Config{
@ -79,8 +79,8 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectPanic: false, expectErr: false,
name: "Monitor failure, successful alert", name: "Monitor failure, successful alert",
}, },
{ {
config: Config{ config: Config{
@ -99,24 +99,16 @@ func TestCheckMonitors(t *testing.T) {
}, },
}, },
}, },
expectPanic: true, expectErr: true,
name: "Monitor failure, bad alert", name: "Monitor failure, bad alert",
}, },
} }
for _, c := range cases { for _, c := range cases {
// Create new function so that deferred recovery can run at end of each test case c.config.Init()
func() { err := checkMonitors(&c.config)
// Set up recover to catch panic if err == nil && c.expectErr {
defer func() { t.Errorf("checkMonitors(%s): Expected panic, the code did not panic", c.name)
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)
}()
} }
} }