Skip to content

Commit

Permalink
fix: run error code propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Aug 29, 2022
1 parent 7973148 commit c912892
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"

"github.com/outblocks/outblocks-cli/cmd"
"github.com/outblocks/outblocks-cli/pkg/actions"
"google.golang.org/grpc/grpclog"
)

Expand Down Expand Up @@ -43,10 +44,18 @@ func main() {
err := exec.Execute(ctx)

if err != nil {
if e, ok := err.(*actions.ErrExit); ok {
if e.Message != "" {
exec.Log().Errorln(e.Message)
}

os.Exit(e.StatusCode) //nolint
}

if ctx.Err() != context.Canceled {
exec.Log().Errorln("Error occurred:", err)
}

os.Exit(1) //nolint: gocritic
os.Exit(1)
}
}
18 changes: 17 additions & 1 deletion pkg/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ const (
healthcheckTimeout = 3 * time.Second
)

type ErrExit struct {
StatusCode int
Message string
}

func (e *ErrExit) Error() string {
return fmt.Sprintf("exit code %d", e.StatusCode)
}

func NewRun(log logger.Logger, cfg *config.Project, opts *RunOptions) *Run {
return &Run{
log: log,
Expand Down Expand Up @@ -751,7 +760,14 @@ func (d *Run) runSelfAsSudo() error {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

return cmd.Run()
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok {
return &ErrExit{
StatusCode: e.ProcessState.ExitCode(),
}
}

return err
}

func (d *Run) Run(ctx context.Context) error {
Expand Down

0 comments on commit c912892

Please sign in to comment.