Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed err object input to getter function #36

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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
}
Loading