diff --git a/runner.go b/runner.go index 8bd0a34..db24a7c 100644 --- a/runner.go +++ b/runner.go @@ -2,6 +2,7 @@ package process import ( "context" + "fmt" "sync" "time" @@ -257,13 +258,25 @@ func (r *Runner) runSpec(ctx context.Context, spec RunSpec) RunResult { <-proc.Done() + var runErr error + switch proc.Status { + case StatusKilled: + runErr = coreerr.E("Runner.runSpec", "process was killed", nil) + case StatusExited: + if proc.ExitCode != 0 { + runErr = coreerr.E("Runner.runSpec", fmt.Sprintf("process exited with code %d", proc.ExitCode), nil) + } + case StatusFailed: + runErr = coreerr.E("Runner.runSpec", "process failed to start", nil) + } + return RunResult{ Name: spec.Name, Spec: spec, ExitCode: proc.ExitCode, Duration: proc.Duration, Output: proc.Output(), - Error: nil, + Error: runErr, } } diff --git a/service.go b/service.go index 6602f3e..ee80a32 100644 --- a/service.go +++ b/service.go @@ -224,7 +224,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce ID: id, ExitCode: -1, Duration: time.Since(startedAt), - Error: nil, + Error: err, }) } return nil, coreerr.E("Service.StartWithOptions", "failed to start process", err) @@ -283,7 +283,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce err := cmd.Wait() duration := time.Since(proc.StartedAt) - status, exitCode, _, signalName := classifyProcessExit(err) + status, exitCode, exitErr, signalName := classifyProcessExit(err) proc.mu.Lock() proc.Duration = duration @@ -301,7 +301,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce ID: id, ExitCode: exitCode, Duration: duration, - Error: nil, + Error: exitErr, } if c := s.coreApp(); c != nil { @@ -629,7 +629,8 @@ func classifyProcessExit(err error) (Status, int, error, string) { } return StatusKilled, -1, coreerr.E("Service.StartWithOptions", "process was killed", nil), signalName } - return StatusExited, exitErr.ExitCode(), nil, "" + exitCode := exitErr.ExitCode() + return StatusExited, exitCode, coreerr.E("Service.StartWithOptions", fmt.Sprintf("process exited with code %d", exitCode), nil), "" } return StatusFailed, 0, err, "" diff --git a/service_test.go b/service_test.go index 08c7332..90a57f2 100644 --- a/service_test.go +++ b/service_test.go @@ -327,7 +327,7 @@ func TestService_Actions(t *testing.T) { defer mu.Unlock() assert.Len(t, exited, 1) assert.Equal(t, proc.ID, exited[0].ID) - assert.Nil(t, exited[0].Error) + assert.Error(t, exited[0].Error) assert.Equal(t, StatusKilled, proc.Status) }) @@ -360,7 +360,7 @@ func TestService_Actions(t *testing.T) { defer mu.Unlock() require.Len(t, exited, 1) assert.Equal(t, -1, exited[0].ExitCode) - assert.Nil(t, exited[0].Error) + assert.Error(t, exited[0].Error) }) }