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"
)
func getErrorMessage(err *exec.ExitError) string {
stderr := string(err.Stderr)
if len(stderr) == 0 {
return err.Error()
} else {
return fmt.Sprintf("%.200s", stderr)
func getErrorMessage(err error) string {
exitErr, ok := err.(*exec.ExitError)
message := err.Error()
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 (
"github.com/sqshq/sampler/config"
"os/exec"
"time"
)
@ -57,7 +56,7 @@ func (s *Sampler) sample(item Item, options config.Options) {
} else {
s.consumer.AlertChannel <- &Alert{
Title: "SAMPLING FAILURE",
Text: getErrorMessage(err.(*exec.ExitError)),
Text: getErrorMessage(err),
Color: item.Color,
}
}

View File

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