Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/exec: add example of handling exec.Error and exec.ExitError #35874

Open
abitrolly opened this issue Nov 27, 2019 · 6 comments
Open

os/exec: add example of handling exec.Error and exec.ExitError #35874

abitrolly opened this issue Nov 27, 2019 · 6 comments
Labels
Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@abitrolly
Copy link

abitrolly commented Nov 27, 2019

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.

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.
@ianlancetaylor 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
@ianlancetaylor ianlancetaylor added Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Nov 28, 2019
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Nov 28, 2019
@ianlancetaylor
Copy link
Contributor

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.

@abitrolly
Copy link
Author

@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.

https://play.golang.org/p/Fk-hFe2Urb0

@ianlancetaylor
Copy link
Contributor

errors.Is takes a value, not a type. It's approximately the == operator. You can't write

    if err == exec.ExitError {

so you can't write

    if errors.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.

@abitrolly
Copy link
Author

I use go run, because tests for os.exec in my scenario with wrapping administrative binaries are either dangerous or too complicated.

@ianlancetaylor
Copy link
Contributor

If you choose to write code with no tests then I suggest that you at least add go vet to your process.

@gopherbot
Copy link

Change https://golang.org/cl/213337 mentions this issue: os/exec: add examples for exec.Error and exec.ExitError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants