You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)?
$ go version
go version go1.13.4 linux/amd64
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.
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.
The text was updated successfully, but these errors were encountered:
ianlancetaylor
changed the title
os.exec: add example of handling exec.Error and exec.ExitError
os/exec: add example of handling exec.Error and exec.ExitError
Nov 28, 2019
FYI, errors.Is expects to match against a value, not a type. errors.Is is useful with a value like io.EOF. It's not useful with a type like exec.ExitError. For a type, you want to use errors.As. Or, in this case, a type assertion.
@ianlancetaylor why can't errors.Is match against type as well? Using errors.As means I need to explicitly create a pointer to the error I need to check. And using errors.As wrong is a source of runtime crashes, similar to Python.
errors.Is takes a value, not a type. It's approximately the == operator. You can't write
iferr== exec.ExitError {
so you can't write
iferrors.Is(err, exec.ExitError) {
Yes, you have to call errors.As with the right kind of argument, but it's not quite a Pythonesque runtime error. Running go vet will report the problem, and go vet is run automatically whenever you run go test.
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: