Skip to content

Commit

Permalink
fix: changed err object input to getter function
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Aug 31, 2024
1 parent a9930b4 commit d2ed2e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
30 changes: 15 additions & 15 deletions core/cmd_connection/docker_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ func (d *DockerCreateConnection) Execute() ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
helpers.WarnOnErr(
d.log,
d.cli.ContainerRemove(ctx, exec.ID,
defer helpers.WarnOnErr(

Check failure on line 131 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 131 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / analyze (go)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 131 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (macos-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 131 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)
d.log,
func() error {
return d.cli.ContainerRemove(ctx, exec.ID,
container.RemoveOptions{
Force: true,
},
),
"cannot remove the container: %s",
)
}()
)
},
"cannot remove the container: %s",
)

for {
err = d.cli.ContainerStart(
Expand Down Expand Up @@ -181,13 +181,13 @@ func (d *DockerCreateConnection) Execute() ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
helpers.WarnOnErr(
d.log,
resp.Close(),
"cannot close the container's logs: %s",
)
}()
defer helpers.WarnOnErr(

Check failure on line 184 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 184 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / analyze (go)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 184 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (macos-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 184 in core/cmd_connection/docker_create.go

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)
d.log,
func() error {
return resp.Close()
},
"cannot close the container's logs: %s",
)

writer := bytes.NewBuffer([]byte{})
// Print the command output
Expand Down
8 changes: 7 additions & 1 deletion core/task/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ func (c *Command) Execute(ctx context.Context) (e error) {
if err := connection.Prepare(cmdCtx, c.task); err != nil {
log.Warn("cannot prepare command: ", err)
ctx = addFailedConnections(ctx, conn)
helpers.WarnOnErr(log, connection.Disconnect(), "Cannot disconnect the command's connection: %s")
helpers.WarnOnErr(

Check failure on line 73 in core/task/command.go

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 73 in core/task/command.go

View workflow job for this annotation

GitHub Actions / analyze (go)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 73 in core/task/command.go

View workflow job for this annotation

GitHub Actions / ci (macos-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)

Check failure on line 73 in core/task/command.go

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Error return value of `helpers.WarnOnErr` is not checked (errcheck)
log,
func() error {
return connection.Disconnect()
},
"Cannot disconnect the command's connection: %s",
)
continue
}

Expand Down
10 changes: 7 additions & 3 deletions core/task/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ func (g *Get) Execute(ctx context.Context) (e error) {
res, err := client.Do(req)
if res != nil {
if res.Body != nil {
defer func() {
helpers.WarnOnErr(log, res.Body.Close(), "cannot close response body: %s")
}()
defer helpers.WarnOnErr(
log,
func() error {
return res.Body.Close()
},
"cannot close response body: %s",
)
}
log = log.WithField("status", res.StatusCode)
log.Infoln("received response with status: ", res.Status)
Expand Down
10 changes: 7 additions & 3 deletions core/task/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ func (p *Post) Execute(ctx context.Context) (e error) {

if res != nil {
if res.Body != nil {
defer func() {
helpers.WarnOnErr(log, res.Body.Close(), "cannot close response body: %s")
}()
defer helpers.WarnOnErr(
log,
func() error {
return res.Body.Close()
},
"cannot close response body: %s",
)
}
log = log.WithField("status", res.StatusCode)
log.Infoln("received response with status: ", res.Status)
Expand Down
8 changes: 7 additions & 1 deletion core/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ func (s *WebServer) Serve() {
}

err := engine.Run(fmt.Sprintf("%s:%d", s.address, s.port))
helpers.FatalOnErr(s.log, err, "Failed to start webserver: %s")
helpers.FatalOnErr(
s.log,
func() error {
return err
},
"Failed to start webserver: %s",
)
}

func (s *WebServer) formatter(params gin.LogFormatterParams) string {
Expand Down
14 changes: 8 additions & 6 deletions helpers/err_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
"github.com/sirupsen/logrus"
)

func PanicOnErr(log *logrus.Entry, err error, message string) {
if err != nil {
func PanicOnErr(log *logrus.Entry, errorCatcher func() error, message string) {
if err := errorCatcher(); err != nil {
log.Panicf(message, err)
}
}

func FatalOnErr(log *logrus.Entry, err error, message string) {
if err != nil {
func FatalOnErr(log *logrus.Entry, errorCatcher func() error, message string) {
if err := errorCatcher(); err != nil {
log.Fatalf(message, err)
}
}

func WarnOnErr(log *logrus.Entry, err error, message string) {
if err != nil {
func WarnOnErr(log *logrus.Entry, errorCatcher func() error, message string) error {
if err := errorCatcher(); err != nil {
log.Warnf(message, err)
return err
}
return nil
}

0 comments on commit d2ed2e1

Please sign in to comment.