os/exec: add example of handling exec.Error and exec.ExitError
#35,874 opened on 2019年11月27日
Repository metrics
- Stars
- (133,883 stars)
- PR merge metrics
- (30d に merged PR はありません)
説明
https://golang.org/pkg/os/exec provides exec.Error and exec.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 says Any returned error will usually be of type *ExitError. However, trying to detect error with errors.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 from dnf 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?
./prog.go:20:15: type exec.ExitError is not an expression
Go build failed.