os/exec: add example of handling exec.Error and exec.ExitError #35874
Labels
Documentation
Issues describing a change to documentation.
help wanted
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
https://golang.org/pkg/os/exec provides
exec.Error
andexec.ExitError
error types and doesn't document how to check and distinguish between them.What version of Go are you using (
go version
)?What did you do?
I am using https://golang.org/pkg/os/exec/#Cmd.Output to capture the output of
dnf
command and check its return code. The doc saysAny returned error will usually be of type *ExitError
. However, trying to detect error witherrors.Is(err, exec.ExitError)
(as mentioned in https://golang.org/pkg/errors/) fails with./prog.go:20:15: type exec.ExitError is not an expression
.https://play.golang.org/p/7hjN2PCyBgB
What did you expect to see?
Expect to separate error when command not found (seems to be
*exec.Error
) and when command run successfully with return code (*exec.ExitError
). Expect to read exit code fromdnf
somehow which is part of its API.Expect to see the example of distinguishing between these two errors and reading the exit code in Output example.
What did you see instead?
The text was updated successfully, but these errors were encountered: