Skip to content

Commit

Permalink
In the event of an exec error return any output we can capture as par…
Browse files Browse the repository at this point in the history
…t of the error.

Move all type definitions together.

PiperOrigin-RevId: 388320089
  • Loading branch information
GitGerby authored and copybara-github committed Aug 2, 2021
1 parent cce5425 commit a528792
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions go/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ type ExecResult struct {
ExitErr error
}

// ExecError holds errors and output from failed subprocess executions.
type ExecError struct {
errmsg string
procresult ExecResult
}

// ExecConfig provides flexible execution configuration.
type ExecConfig struct {
// A verifier, if specified, will attempt to verify the subprocess output and return an error if problems are detected.
Verifier *ExecVerifier

// A timeout will kill the subprocess if it doesn't execute within the duration. Leave nil to disable timeout.
Timeout *time.Duration

// If RetryCount is non-zero, Exec will attempt to retry a failed execution (one which returns err). Executions will retry
// every RetryInterval. Combine with a Verifier to retry until certain conditions are met.
RetryCount int
RetryInterval *time.Duration

SpAttr *syscall.SysProcAttr
}

var (
// ErrExitCode indicates an exit-code related failure
ErrExitCode = errors.New("produced invalid exit code")
Expand All @@ -63,20 +85,14 @@ var (
fnSleep = time.Sleep
)

// ExecConfig provides flexible execution configuration.
type ExecConfig struct {
// A verifier, if specified, will attempt to verify the subprocess output and return an error if problems are detected.
Verifier *ExecVerifier

// A timeout will kill the subprocess if it doesn't execute within the duration. Leave nil to disable timeout.
Timeout *time.Duration

// If RetryCount is non-zero, Exec will attempt to retry a failed execution (one which returns err). Executions will retry
// every RetryInterval. Combine with a Verifier to retry until certain conditions are met.
RetryCount int
RetryInterval *time.Duration
// Satisfies the Error interface and returns the simple error string associated with the execution.
func (e *ExecError) Error() string {
return e.errmsg
}

SpAttr *syscall.SysProcAttr
// Result returns any information that could be captured from the subprocess that was executed.
func (e *ExecError) Result() ExecResult {
return e.procresult
}

// Exec executes a subprocess and returns the results.
Expand Down Expand Up @@ -249,7 +265,10 @@ func execute(path string, args []string, conf *ExecConfig) (ExecResult, error) {

// when the execution times out return a timeout error
if conf.Timeout != nil && !timer.Stop() {
return result, ErrTimeout
return result, &ExecError{
errmsg: ErrTimeout.Error(),
procresult: result,
}
}

result.ExitCode = cmd.ProcessState.ExitCode()
Expand Down

0 comments on commit a528792

Please sign in to comment.