Skip to content

Commit

Permalink
use custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
KollaAdithya committed Dec 16, 2023
1 parent cd497f9 commit 08542d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
12 changes: 11 additions & 1 deletion internal/pkg/docker/dockerengine/dockerengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (c DockerCmdClient) ContainerExitCode(ctx context.Context, name string) (in
return 0, err
}
if state.Status == containerStatusRunning {
return 0, fmt.Errorf("container %q has not exited", name)
return 0, &ErrContainerNotExited{name: name}
}
return state.ExitCode, nil
}
Expand Down Expand Up @@ -614,3 +614,13 @@ type errEmptyImageTags struct {
func (e *errEmptyImageTags) Error() string {
return fmt.Sprintf("tags to reference an image should not be empty for building and pushing into the ECR repository %s", e.uri)
}

// ErrContainerNotExited represents an error when a Docker container has not exited.
type ErrContainerNotExited struct {
name string
}

// Error returns the error message.
func (e *ErrContainerNotExited) Error() string {
return fmt.Sprintf("container %q has not exited", e.name)
}
6 changes: 4 additions & 2 deletions internal/pkg/docker/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ func (o *Orchestrator) waitForContainerDependencies(ctx context.Context, name st
}
case ctrStateComplete:
exitCode, err := o.docker.ContainerExitCode(ctx, ctrId)
if errors.Is(err, fmt.Errorf("container %q has not exited", name)) {
var errContainerNotExited *dockerengine.ErrContainerNotExited
if errors.As(err, &errContainerNotExited) {
continue
}
if err != nil {
Expand All @@ -546,7 +547,8 @@ func (o *Orchestrator) waitForContainerDependencies(ctx context.Context, name st
}
case ctrStateSuccess:
exitCode, err := o.docker.ContainerExitCode(ctx, ctrId)
if errors.Is(err, fmt.Errorf("container %q has not exited", name)) {
var errContainerNotExited *dockerengine.ErrContainerNotExited
if errors.As(err, &errContainerNotExited) {
continue
}
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/docker/orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func TestOrchestrator(t *testing.T) {
if name == "prefix-foo" {
return 143, fmt.Errorf("some error")
}
return 0, fmt.Errorf("container %q has not exited", name)
return 0, &dockerengine.ErrContainerNotExited{}
},
}
return func(t *testing.T, o *Orchestrator) {
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestOrchestrator(t *testing.T) {
if containerName == "prefix-foo" {
return 143, nil
}
return 0, fmt.Errorf("container %q has not exited", containerName)
return 0, &dockerengine.ErrContainerNotExited{}
},
}
return func(t *testing.T, o *Orchestrator) {
Expand Down Expand Up @@ -515,7 +515,7 @@ func TestOrchestrator(t *testing.T) {
return true, nil
},
ContainerExitCodeFn: func(ctx context.Context, containerName string) (int, error) {
return -1, nil
return 0, nil
},
RunFn: func(ctx context.Context, opts *dockerengine.RunOptions) error {
if opts.ContainerName == "prefix-foo" {
Expand Down

0 comments on commit 08542d3

Please sign in to comment.