Skip to content

Commit

Permalink
Remove exit error types (#352)
Browse files Browse the repository at this point in the history
* remove exit error types

* reinstate error wrapping for context errs
  • Loading branch information
kmoe authored Feb 20, 2023
1 parent 0fb01be commit a7e018d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 451 deletions.
22 changes: 14 additions & 8 deletions tfexec/cmd_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ 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 tf.wrapExitError(ctx, err, "")
return err
}

var errStdout, errStderr error
Expand All @@ -66,19 +69,22 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
wg.Wait()

err = cmd.Wait()
if err == nil && ctx.Err() != nil {
err = ctx.Err()
if ctx.Err() != nil {
return cmdErr{
err: err,
ctxErr: ctx.Err(),
}
}
if err != nil {
return tf.wrapExitError(ctx, err, errBuf.String())
return err
}

// Return error if there was an issue reading the std out/err
if errStdout != nil && ctx.Err() != nil {
return tf.wrapExitError(ctx, errStdout, errBuf.String())
return errStdout
}
if errStderr != nil && ctx.Err() != nil {
return tf.wrapExitError(ctx, errStderr, errBuf.String())
return errStderr
}

return nil
Expand Down
22 changes: 14 additions & 8 deletions tfexec/cmd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ 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 tf.wrapExitError(ctx, err, "")
return err
}

var errStdout, errStderr error
Expand All @@ -71,19 +74,22 @@ func (tf *Terraform) runTerraformCmd(ctx context.Context, cmd *exec.Cmd) error {
wg.Wait()

err = cmd.Wait()
if err == nil && ctx.Err() != nil {
err = ctx.Err()
if ctx.Err() != nil {
return cmdErr{
err: err,
ctxErr: ctx.Err(),
}
}
if err != nil {
return tf.wrapExitError(ctx, err, errBuf.String())
return err
}

// Return error if there was an issue reading the std out/err
if errStdout != nil && ctx.Err() != nil {
return tf.wrapExitError(ctx, errStdout, errBuf.String())
return errStdout
}
if errStderr != nil && ctx.Err() != nil {
return tf.wrapExitError(ctx, errStderr, errBuf.String())
return errStderr
}

return nil
Expand Down
27 changes: 26 additions & 1 deletion tfexec/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tfexec

import "fmt"
import (
"context"
"fmt"
)

// this file contains non-parsed exported errors

Expand Down Expand Up @@ -37,3 +40,25 @@ type ErrManualEnvVar struct {
func (err *ErrManualEnvVar) Error() string {
return fmt.Sprintf("manual setting of env var %q detected", err.Name)
}

// 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.Canceled
}
return false
}

func (e cmdErr) Error() string {
return e.err.Error()
}
Loading

0 comments on commit a7e018d

Please sign in to comment.