Skip to content

Commit a00c1a5

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

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

tfexec/cmd_default.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ 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+
1331
func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
1432
var errBuf strings.Builder
1533

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

4260
err = cmd.Start()
43-
if err == nil && ctx.Err() != nil {
44-
err = ctx.Err()
61+
if ctx.Err() != nil {
62+
return cmdErr{
63+
err: err,
64+
ctxErr: ctx.Err(),
65+
}
4566
}
4667
if err != nil {
4768
return err

0 commit comments

Comments
 (0)