Propagate process exit errors

This commit is contained in:
Virgil 2026-04-04 02:24:32 +00:00
parent 98fe626d8e
commit ec2a6838b8
3 changed files with 21 additions and 7 deletions

View file

@ -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,
}
}

View file

@ -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, ""

View file

@ -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)
})
}