Skip to content

Commit a49cfbf

Browse files
committed
reinstate error wrapping for context errs
1 parent 0934b50 commit a49cfbf

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

tfexec/cmd_default.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ import (
1010
"sync"
1111
)
1212

13+
// cmdErr is a custom error type to be returned when a cmd exits with a context
14+
// error such as context.Canceled or context.DeadlineExceeded.
15+
// The type is specifically designed to respond true to errors.Is for these two
16+
// errors.
17+
// See https://github.com/golang/go/issues/21880 for why this is necessary.
18+
type cmdErr struct {
19+
err error
20+
ctxErr error
21+
}
22+
23+
func (e *cmdErr) Is(target error) bool {
24+
switch target {
25+
case context.DeadlineExceeded, context.Canceled:
26+
return e.ctxErr == context.DeadlineExceeded || e.ctxErr == context.Cancelled
27+
}
28+
return false
29+
}
30+
31+
func (e *cmdErr) Error() string {
32+
return e.err.Error()
33+
}
34+
1335
func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
1436
var errBuf strings.Builder
1537

@@ -40,8 +62,11 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
4062
}
4163

4264
err = cmd.Start()
43-
if err == nil && ctx.Err() != nil {
44-
err = ctx.Err()
65+
if ctx.Err() != nil {
66+
return cmdErr{
67+
err: err,
68+
ctxErr: ctx.Err(),
69+
}
4570
}
4671
if err != nil {
4772
return err

0 commit comments

Comments
 (0)