Skip to content

Commit

Permalink
reinstate error wrapping for context errs
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Feb 14, 2023
1 parent 0934b50 commit a00c1a5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tfexec/cmd_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ import (
"sync"
)

// cmdErr is a custom error type to be returned when a cmd exits with a context
// error such as context.Canceled or context.DeadlineExceeded.
// The type is specifically designed to respond true to errors.Is for these two
// errors.
// See https://github.com/golang/go/issues/21880 for why this is necessary.
type cmdErr struct {
err error
ctxErr error
}

func (e *cmdErr) Is(target error) bool {
switch target {
case context.DeadlineExceeded, context.Canceled:
return e.ctxErr == context.DeadlineExceeded || e.ctxErr == context.Cancelled
}
return false
}

func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
var errBuf strings.Builder

Expand Down Expand Up @@ -40,8 +58,11 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
}

err = cmd.Start()
if err == nil && ctx.Err() != nil {
err = ctx.Err()
if ctx.Err() != nil {
return cmdErr{
err: err,
ctxErr: ctx.Err(),
}
}
if err != nil {
return err
Expand Down

0 comments on commit a00c1a5

Please sign in to comment.