pkg/notifier: improve error handling messages

This commit is contained in:
mathetake
2020-03-08 11:53:03 +09:00
parent 23ab1bdb4b
commit 0be72ab981
6 changed files with 13 additions and 15 deletions
+4 -4
View File
@@ -13,14 +13,14 @@ import (
func postMessage(address string, payload interface{}) error {
data, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshalling notification payload failed %v", err)
return fmt.Errorf("marshalling notification payload failed: %w", err)
}
b := bytes.NewBuffer(data)
req, err := http.NewRequest("POST", address, b)
if err != nil {
return err
return fmt.Errorf("http.NewRequest failed: %w", err)
}
req.Header.Set("Content-type", "application/json")
@@ -29,14 +29,14 @@ func postMessage(address string, payload interface{}) error {
res, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return fmt.Errorf("sending notification failed %v", err)
return fmt.Errorf("sending notification failed: %w", err)
}
defer res.Body.Close()
statusCode := res.StatusCode
if statusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("sending notification failed %v", string(body))
return fmt.Errorf("sending notification failed: %s", string(body))
}
return nil
+1 -1
View File
@@ -74,7 +74,7 @@ func (s *Discord) Post(workload string, namespace string, message string, fields
err := postMessage(s.URL, payload)
if err != nil {
return err
return fmt.Errorf("postMessage failed: %w", err)
}
return nil
+5 -5
View File
@@ -17,14 +17,14 @@ func NewFactory(URL string, username string, channel string) *Factory {
}
func (f Factory) Notifier(provider string) (Interface, error) {
switch {
case provider == "slack":
switch provider {
case "slack":
return NewSlack(f.URL, f.Username, f.Channel)
case provider == "discord":
case "discord":
return NewDiscord(f.URL, f.Username, f.Channel)
case provider == "rocket":
case "rocket":
return NewRocket(f.URL, f.Username, f.Channel)
case provider == "msteams":
case "msteams":
return NewMSTeams(f.URL)
}
+1 -2
View File
@@ -65,8 +65,7 @@ func (s *Rocket) Post(workload string, namespace string, message string, fields
err := postMessage(s.URL, payload)
if err != nil {
return err
return fmt.Errorf("postMessage failed: %w", err)
}
return nil
}
+1 -2
View File
@@ -90,8 +90,7 @@ func (s *Slack) Post(workload string, namespace string, message string, fields [
err := postMessage(s.URL, payload)
if err != nil {
return err
return fmt.Errorf("postMessage failed: %w", err)
}
return nil
}
+1 -1
View File
@@ -70,7 +70,7 @@ func (s *MSTeams) Post(workload string, namespace string, message string, fields
err := postMessage(s.URL, payload)
if err != nil {
return err
return fmt.Errorf("postMessage failed: %w", err)
}
return nil