From 1d03fec75618b9e6809312cff5785b75705d37c7 Mon Sep 17 00:00:00 2001 From: Luiz Amaral Date: Fri, 18 Aug 2023 14:24:13 +0200 Subject: [PATCH] Improve error handling --- command/ripeatlas/credits.go | 9 ++++----- command/ripeatlas/traceroute.go | 11 +++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/command/ripeatlas/credits.go b/command/ripeatlas/credits.go index ecbb9d5c..8b4836ad 100644 --- a/command/ripeatlas/credits.go +++ b/command/ripeatlas/credits.go @@ -9,7 +9,6 @@ import ( "github.com/innogames/slack-bot/v2/bot/matcher" "github.com/innogames/slack-bot/v2/bot/msg" "github.com/innogames/slack-bot/v2/client" - "github.com/pkg/errors" ) type creditsCommand struct { @@ -30,7 +29,7 @@ func (c *creditsCommand) credits(_ matcher.Result, message msg.Message) { url := fmt.Sprintf("%s/credits", c.cfg.APIURL) req, err := http.NewRequest("GET", url, nil) if err != nil { - c.ReplyError(message, errors.Wrap(err, "Request creation returned an err")) + c.ReplyError(message, fmt.Errorf("request creation returned an err: %w", err)) return } @@ -39,13 +38,13 @@ func (c *creditsCommand) credits(_ matcher.Result, message msg.Message) { response, err := client.GetHTTPClient().Do(req) if err != nil { - c.ReplyError(message, errors.Wrap(err, "Api call returned an err")) + c.ReplyError(message, fmt.Errorf("API call returned an err: %w", err)) return } defer response.Body.Close() - if response.StatusCode >= 300 { - c.SendMessage(message, fmt.Sprintf("Api call returned an err: %d", response.StatusCode)) + if response.StatusCode >= 400 { + c.ReplyError(message, fmt.Errorf("API call returned an err: %d", response.StatusCode)) return } diff --git a/command/ripeatlas/traceroute.go b/command/ripeatlas/traceroute.go index de383dea..688848c5 100644 --- a/command/ripeatlas/traceroute.go +++ b/command/ripeatlas/traceroute.go @@ -14,7 +14,6 @@ import ( "github.com/innogames/slack-bot/v2/bot/matcher" "github.com/innogames/slack-bot/v2/bot/msg" "github.com/innogames/slack-bot/v2/client" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/slack-go/slack" ) @@ -75,7 +74,7 @@ func (c *tracerouteCommand) traceroute(match matcher.Result, message msg.Message url := fmt.Sprintf("%s/measurements", c.cfg.APIURL) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { - c.ReplyError(message, errors.Wrap(err, "Request creation returned an err")) + c.ReplyError(message, fmt.Errorf("request creation returned an err: %w", err)) return } @@ -84,13 +83,13 @@ func (c *tracerouteCommand) traceroute(match matcher.Result, message msg.Message response, err := client.GetHTTPClient().Do(req) if err != nil { - c.ReplyError(message, errors.Wrap(err, "HTTP Client Error")) + c.ReplyError(message, fmt.Errorf("HTTP Client Error: %w", err)) return } defer response.Body.Close() - if response.StatusCode >= 300 { - c.ReplyError(message, errors.New(fmt.Sprintf("Api call returned an err: %d", response.StatusCode))) + if response.StatusCode >= 400 { + c.ReplyError(message, fmt.Errorf("API call returned an err: %d", response.StatusCode)) return } @@ -100,7 +99,7 @@ func (c *tracerouteCommand) traceroute(match matcher.Result, message msg.Message err = json.Unmarshal(body, &measurementResult) if err != nil { - c.ReplyError(message, errors.Wrap(err, "Error unmarshalling MeasurementResult")) + c.ReplyError(message, fmt.Errorf("error unmarshalling MeasurementResult: %w", err)) return }