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

Comments

@abitrolly
Copy link

@abitrolly 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 this to the Backlog milestone Nov 28, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

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

@abitrolly

This comment has been minimized.

Copy link
Author

@abitrolly abitrolly commented Nov 28, 2019

@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

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 2, 2019

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

This comment has been minimized.

Copy link
Author

@abitrolly abitrolly commented Dec 3, 2019

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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 3, 2019

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

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 5, 2020

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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.