diff options
author | Vitaliy Potyarkin <v.potyarkin@yadro.com> | 2024-12-28 10:52:17 +0100 |
---|---|---|
committer | Vitaliy Potyarkin <v.potyarkin@yadro.com> | 2025-01-09 10:36:30 +0100 |
commit | cf6eb354f5f367c6d34eaac22d0e9c257e16405f (patch) | |
tree | 1aeaa6f3a2e6d1c128fa7ca9d27796ee217d87c0 | |
parent | Merge pull request 'feat: export runtime token as FORGEJO_TOKEN' (#88) from x... (diff) | |
download | forgejo-act-cf6eb354f5f367c6d34eaac22d0e9c257e16405f.tar.xz forgejo-act-cf6eb354f5f367c6d34eaac22d0e9c257e16405f.zip |
refactor: Improve logging of error messages
Logging bare errors might lead to long troubleshooting sessions when error
is not specific enough. This commit adds helpful context messages to
logged errors.
Signed-off-by: Vitaliy Potyarkin <v.potyarkin@yadro.com>
-rw-r--r-- | pkg/artifacts/server.go | 2 | ||||
-rw-r--r-- | pkg/container/docker_run.go | 16 |
2 files changed, 11 insertions, 7 deletions
diff --git a/pkg/artifacts/server.go b/pkg/artifacts/server.go index 4b88ea4..160dd4d 100644 --- a/pkg/artifacts/server.go +++ b/pkg/artifacts/server.go @@ -300,7 +300,7 @@ func Serve(ctx context.Context, artifactPath string, addr string, port string) c go func() { logger.Infof("Start server on http://%s:%s", addr, port) if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - logger.Fatal(err) + logger.Fatalf("http listener: %v", err) } }() diff --git a/pkg/container/docker_run.go b/pkg/container/docker_run.go index 3232c14..08bf53d 100644 --- a/pkg/container/docker_run.go +++ b/pkg/container/docker_run.go @@ -526,8 +526,9 @@ func (cr *containerReference) extractFromImageEnv(env *map[string]string) common inspect, _, err := cr.cli.ImageInspectWithRaw(ctx, cr.input.Image) if err != nil { + err = fmt.Errorf("inspect image: %w", err) logger.Error(err) - return fmt.Errorf("inspect image: %w", err) + return err } if inspect.Config == nil { @@ -536,8 +537,9 @@ func (cr *containerReference) extractFromImageEnv(env *map[string]string) common imageEnv, err := godotenv.Unmarshal(strings.Join(inspect.Config.Env, "\n")) if err != nil { + err = fmt.Errorf("unmarshal image env: %w", err) logger.Error(err) - return fmt.Errorf("unmarshal image env: %w", err) + return err } for k, v := range imageEnv { @@ -709,7 +711,7 @@ func (cr *containerReference) waitForCommand(ctx context.Context, isTerminal boo return ctx.Err() case err := <-cmdResponse: if err != nil { - logger.Error(err) + logger.Errorf("command response: %v", err) } return nil @@ -754,11 +756,11 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgno name := tarFile.Name() err := tarFile.Close() if err != nil && !errors.Is(err, os.ErrClosed) { - logger.Error(err) + logger.Errorf("close tar file: %s: %v", name, err) } err = os.Remove(name) if err != nil { - logger.Error(err) + logger.Errorf("remove file: %s: %v", name, err) } }(tarFile) tw := tar.NewWriter(tarFile) @@ -797,6 +799,8 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgno return err } if err := tw.Close(); err != nil { + err = fmt.Errorf("close tar writer: %w", err) + logger.Debug(err) return err } @@ -875,7 +879,7 @@ func (cr *containerReference) attach() common.Executor { _, err = io.Copy(outWriter, out.Reader) } if err != nil { - common.Logger(ctx).Error(err) + common.Logger(ctx).Errorf("redirect container output: %v", err) } }() return nil |