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
It is impossible to use errors.Is() with exec.ExitError as the result of a command execution. Consider the following:
import (
"errors""log""os/exec"
)
varerrerrorfuncmain() {
varcmd*exec.Cmdcmd=exec.Command("somecommand", "somearg1")
iferr=cmd.Start(); err!=nil {
log.Panicln(err)
}
iferr=cmd.Wait(); err!=nil {
iferrors.Is(err, exec.ExitError) { // THIS WILL FAIL SYNTAX, as exec.ExitError.Error() expects a pointer receiver._=""// Do something here.
}
}
// ...
}
Because the structure field values that the *exec.ExitError contains are unpredictable, we cannot do a direct comparison by creating an exec.ExitError to compare against.
Because .Wait() (and .Run, etc.) may return errors other than *exec.ExitError, we can't simply assign the return as an *exec.ExitError (and indeed, the returned is an error interface).
This means in order to simply to determine if it's a runtime non-exec.ExitError error or a non-zero status on Linux (et. al.), one must use typeswitching or typecasting, use errors.As and check the bool of that return with an explicitly declared var for the .As(), or just simply use .Run() and hope that your command returns 0.
This is immensely cumbersome for something that oughtn't be, considering the incredible prevalence of os/exec.Command executions in many third-party libraries.
What did you expect to see?
N/A
What did you see instead?
N/A
The text was updated successfully, but these errors were encountered:
Is for ExitError wouldn't be useful as it can't convey information specific to the error.
Using As is also not much longer than Is: if e := (&os.ExitError{}); errors.As(err, &e) {}
Or are we back to "typeswitching is the best practice for determining an error type" and just abandoning errors.Is being current recommendation? If I need specific logic that depends on whether that response came from the command being executed or from go itself, I am expected to then capture that with an errors.As?
That strikes me as backwards, cumbersome, and overcomplicated.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
It is impossible to use errors.Is() with exec.ExitError as the result of a command execution. Consider the following:
Because the structure field values that the
*exec.ExitError
contains are unpredictable, we cannot do a direct comparison by creating an exec.ExitError to compare against.Because .Wait() (and .Run, etc.) may return errors other than
*exec.ExitError
, we can't simply assign the return as an*exec.ExitError
(and indeed, the returned is anerror
interface).This means in order to simply to determine if it's a runtime non-exec.ExitError error or a non-zero status on Linux (et. al.), one must use typeswitching or typecasting, use
errors.As
and check the bool of that return with an explicitly declared var for the .As(), or just simply use.Run()
and hope that your command returns 0.This is immensely cumbersome for something that oughtn't be, considering the incredible prevalence of
os/exec.Command
executions in many third-party libraries.What did you expect to see?
N/A
What did you see instead?
N/A
The text was updated successfully, but these errors were encountered: