diff --git a/core/cmd_connection/docker_create.go b/core/cmd_connection/docker_create.go index 982f7c6..a5f5928 100644 --- a/core/cmd_connection/docker_create.go +++ b/core/cmd_connection/docker_create.go @@ -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( + 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( @@ -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( + d.log, + func() error { + return resp.Close() + }, + "cannot close the container's logs: %s", + ) writer := bytes.NewBuffer([]byte{}) // Print the command output diff --git a/core/task/command.go b/core/task/command.go index 7efb168..92b8e82 100644 --- a/core/task/command.go +++ b/core/task/command.go @@ -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( + log, + func() error { + return connection.Disconnect() + }, + "Cannot disconnect the command's connection: %s", + ) continue } diff --git a/core/task/get.go b/core/task/get.go index 96b2685..ed616dc 100644 --- a/core/task/get.go +++ b/core/task/get.go @@ -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) diff --git a/core/task/post.go b/core/task/post.go index f76fd3f..dce113f 100644 --- a/core/task/post.go +++ b/core/task/post.go @@ -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) diff --git a/core/webserver/webserver.go b/core/webserver/webserver.go index 5ff7ebb..76f469c 100644 --- a/core/webserver/webserver.go +++ b/core/webserver/webserver.go @@ -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 { diff --git a/helpers/err_handlers.go b/helpers/err_handlers.go index 9567acb..f570aa3 100644 --- a/helpers/err_handlers.go +++ b/helpers/err_handlers.go @@ -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 }