Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lamaral committed Aug 18, 2023
1 parent 3fb3602 commit 1d03fec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions command/ripeatlas/credits.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/innogames/slack-bot/v2/bot/matcher"

Check failure on line 9 in command/ripeatlas/credits.go

View workflow job for this annotation

GitHub Actions / Lint

import 'github.com/innogames/slack-bot/v2/bot/matcher' is not allowed from list 'Main' (depguard)
"github.com/innogames/slack-bot/v2/bot/msg"

Check failure on line 10 in command/ripeatlas/credits.go

View workflow job for this annotation

GitHub Actions / Lint

import 'github.com/innogames/slack-bot/v2/bot/msg' is not allowed from list 'Main' (depguard)
"github.com/innogames/slack-bot/v2/client"

Check failure on line 11 in command/ripeatlas/credits.go

View workflow job for this annotation

GitHub Actions / Lint

import 'github.com/innogames/slack-bot/v2/client' is not allowed from list 'Main' (depguard)
"github.com/pkg/errors"
)

type creditsCommand struct {
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions command/ripeatlas/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand All @@ -84,13 +83,13 @@ func (c *tracerouteCommand) traceroute(match matcher.Result, message msg.Message

response, err := client.GetHTTPClient().Do(req)

Check failure on line 84 in command/ripeatlas/traceroute.go

View workflow job for this annotation

GitHub Actions / Lint

response body must be closed (bodyclose)
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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 1d03fec

Please sign in to comment.