Skip to content

Commit

Permalink
fix: improve docker error handling (#3942)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Apr 13, 2023
1 parent f6b5e9a commit e872e45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/pipe/docker/docker.go
Expand Up @@ -199,7 +199,7 @@ func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.A

log.Info("building docker image")
if err := imagers[docker.Use].Build(ctx, tmp, images, buildFlags); err != nil {
if strings.Contains(err.Error(), "file not found") || strings.Contains(err.Error(), "not found: not found") {
if isFileNotFoundError(err.Error()) {
var files []string
_ = filepath.Walk(tmp, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
Expand Down Expand Up @@ -238,6 +238,14 @@ Previous error:
return nil
}

func isFileNotFoundError(out string) bool {
if strings.Contains(out, `executable file not found in $PATH`) {
return false
}
return strings.Contains(out, "file not found") ||
strings.Contains(out, "not found: not found")
}

func processImageTemplates(ctx *context.Context, docker config.Docker) ([]string, error) {
// nolint:prealloc
var images []string
Expand Down
11 changes: 11 additions & 0 deletions internal/pipe/docker/docker_test.go
Expand Up @@ -1428,3 +1428,14 @@ func TestDependencies(t *testing.T) {
require.Equal(t, []string{"docker", "docker"}, Pipe{}.Dependencies(ctx))
require.Equal(t, []string{"docker", "docker"}, ManifestPipe{}.Dependencies(ctx))
}

func TestIsFileNotFoundError(t *testing.T) {
t.Run("executable not in path", func(t *testing.T) {
require.False(t, isFileNotFoundError(`error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH, out:`))
})

t.Run("file not found", func(t *testing.T) {
require.True(t, isFileNotFoundError(`./foo: file not found`))
require.True(t, isFileNotFoundError(`./foo: not found: not found`))
})
}

0 comments on commit e872e45

Please sign in to comment.