mirror of
https://github.com/fluxcd/flagger.git
synced 2026-04-15 06:57:34 +00:00
pkg/notifier: improve error handling messages
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user