From 0be72ab981fe9fe1105fec9cafdf8f47daf56967 Mon Sep 17 00:00:00 2001 From: mathetake Date: Sun, 8 Mar 2020 11:53:03 +0900 Subject: [PATCH] pkg/notifier: improve error handling messages --- pkg/notifier/client.go | 8 ++++---- pkg/notifier/discord.go | 2 +- pkg/notifier/factory.go | 10 +++++----- pkg/notifier/rocket.go | 3 +-- pkg/notifier/slack.go | 3 +-- pkg/notifier/teams.go | 2 +- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/notifier/client.go b/pkg/notifier/client.go index 72d0b1bd..b966368d 100644 --- a/pkg/notifier/client.go +++ b/pkg/notifier/client.go @@ -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 diff --git a/pkg/notifier/discord.go b/pkg/notifier/discord.go index 952f45e9..61e45346 100644 --- a/pkg/notifier/discord.go +++ b/pkg/notifier/discord.go @@ -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 diff --git a/pkg/notifier/factory.go b/pkg/notifier/factory.go index deabe4b8..8bcab4df 100644 --- a/pkg/notifier/factory.go +++ b/pkg/notifier/factory.go @@ -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) } diff --git a/pkg/notifier/rocket.go b/pkg/notifier/rocket.go index 4bdf549f..d15fef89 100644 --- a/pkg/notifier/rocket.go +++ b/pkg/notifier/rocket.go @@ -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 } diff --git a/pkg/notifier/slack.go b/pkg/notifier/slack.go index 52084e3e..88e4fb5e 100644 --- a/pkg/notifier/slack.go +++ b/pkg/notifier/slack.go @@ -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 } diff --git a/pkg/notifier/teams.go b/pkg/notifier/teams.go index 63e6ade3..c08381ca 100644 --- a/pkg/notifier/teams.go +++ b/pkg/notifier/teams.go @@ -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