error handling enhancements

This commit is contained in:
sqshq 2019-03-21 23:21:00 -04:00
parent ee914fade3
commit 59f6cda4d6
3 changed files with 14 additions and 9 deletions

View File

@ -5,11 +5,17 @@ import (
"os/exec" "os/exec"
) )
func getErrorMessage(err *exec.ExitError) string { func getErrorMessage(err error) string {
stderr := string(err.Stderr)
if len(stderr) == 0 { exitErr, ok := err.(*exec.ExitError)
return err.Error() message := err.Error()
} else {
return fmt.Sprintf("%.200s", stderr) if ok {
stderr := string(exitErr.Stderr)
if len(stderr) != 0 {
message = fmt.Sprintf("%.200s", stderr)
}
} }
return message
} }

View File

@ -2,7 +2,6 @@ package data
import ( import (
"github.com/sqshq/sampler/config" "github.com/sqshq/sampler/config"
"os/exec"
"time" "time"
) )
@ -57,7 +56,7 @@ func (s *Sampler) sample(item Item, options config.Options) {
} else { } else {
s.consumer.AlertChannel <- &Alert{ s.consumer.AlertChannel <- &Alert{
Title: "SAMPLING FAILURE", Title: "SAMPLING FAILURE",
Text: getErrorMessage(err.(*exec.ExitError)), Text: getErrorMessage(err),
Color: item.Color, Color: item.Color,
} }
} }

View File

@ -107,7 +107,7 @@ func (t *Trigger) evaluate(sample *Sample) bool {
if err != nil { if err != nil {
t.consumer.AlertChannel <- &Alert{ t.consumer.AlertChannel <- &Alert{
Title: "TRIGGER CONDITION FAILURE", Title: "TRIGGER CONDITION FAILURE",
Text: getErrorMessage(err.(*exec.ExitError)), Text: getErrorMessage(err),
} }
} }