Skip to content

Commit 877a688

Browse files
committed
do not do a hard os.Exit when subcommand fails
1 parent 8afab97 commit 877a688

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

httptap.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ func Main() error {
191191
err := cmd.Run()
192192
// if the subprocess exited with an error code then do not print any
193193
// extra information but do exit with the same code
194-
if exiterr, ok := err.(*exec.ExitError); ok {
195-
os.Exit(exiterr.ExitCode())
196-
}
197194
if err != nil {
198195
return fmt.Errorf("error re-executing ourselves in a new user namespace: %w", err)
199196
}
@@ -232,9 +229,6 @@ func Main() error {
232229
cmd.Stdout = os.Stdout
233230
cmd.Stderr = os.Stderr
234231
err := cmd.Run()
235-
if exiterr, ok := err.(*exec.ExitError); ok {
236-
os.Exit(exiterr.ExitCode())
237-
}
238232
if err != nil {
239233
return fmt.Errorf("error launching final subprocess from third stage: %w", err)
240234
}
@@ -807,12 +801,7 @@ func Main() error {
807801
// wait for the subprocess to complete
808802
err = cmd.Wait()
809803
if err != nil {
810-
exitError, isExitError := err.(*exec.ExitError)
811-
if isExitError {
812-
os.Exit(exitError.ExitCode())
813-
} else {
814-
return fmt.Errorf("error running subprocess: %v", err)
815-
}
804+
return fmt.Errorf("error running subprocess: %w", err)
816805
}
817806
return nil
818807
}
@@ -822,6 +811,14 @@ func main() {
822811
log.SetFlags(0)
823812
err := Main()
824813
if err != nil {
814+
// if we exit due to a subprocess returning with non-zero exit code then do not
815+
// print any extraneous output but do exit with the same code
816+
var exitError *exec.ExitError
817+
if errors.As(err, &exitError) {
818+
os.Exit(exitError.ExitCode())
819+
}
820+
821+
// for any other kind of error, print the error and exit with code 1
825822
log.Fatal(err)
826823
}
827824
}

0 commit comments

Comments
 (0)