PowerShell-RFC
PowerShell-RFC copied to clipboard
Native Command Error Handling
Link to original: #261
In Bash, it is possible to temporarily disable set -e
by using command || true
, e.g.
set -e
cat ./nonexistent || true
echo "Test" # will be written
From the specification, it is unclear whether a similar trick will work with $PSNativeCommandErrorAction
. I believe it should be written explicitly.
@ForNeVeR you would do this:
$PSNativeCommandErrorAction = $true
& {
$PSNativeCommandErrorAction = $false
cat ./nonexistent
"Test" # will be written
}
cat ./nonexistent
"Will not get output"
The disabling of $PSNativeCommandErrorAction
will only be valid for that scriptblock.
@theJasonHelmick When can we get this? Please let this ship in 7.2 - at least as an experimental feature!!! Maybe I should submit a PR to remove Start-NativeExecution
from the PowerShell build scripts. I think the team isn't feeling the pain as much as the rest of us in the community. :-)
@PowerShell/powershell-committee reviewed this and are okay to move forward with an experimental implementation based on this RFC.
However, it's not currently committed for 7.2. As such, we're adding the up-for-grabs label.
@theJasonHelmick The RFC does not mention anything about this being an experimental feature. I don't think that it needs to be experimental since it is "opt-in" via setting $PSNativeCommandUseErrorActionPreference
to $true
. Just wanted to get clarification on this. Do you agree?
@theJasonHelmick Have you seen this ApplicationFailedException? This covers the case where a system exception is thrown when PS tries to run the application. My concern is that someone looking at the code base may be confused by seeing ApplicationFailedException
and NativeCommandException
.
@rkeithhill even as an opt-in feature, it still needs to be marked as an experimental feature to denote to end-users that the design is not final and may change in the future.
Ideally, it should be code-fenced such that the opt-in isn't possible without the experimental feature turned on, but if the Maintainers agree with you that it's prohibitively difficult to do that, it should still have a placeholder experimental feature (where turning it off would do nothing) to denote the potential instability of the interface.
@rkeithhill agreement that, especially given what's in the docs today for ApplicationFailedException
("Defines the exception that is thrown if a native command fails", it might make sense to rename NativeCommandException
to something else.
@SteveL-MSFT is suggesting NativeCommandExitException
, but we're open to other possibilities.
We're also referring this RFC to the Engine WG for review.
~~Why not re-use ApplicationFailedException if it is about exit?~~
@theJasonHelmick - would you please confirm that the implemented experimental feature matches what is written here?
Is this now part of PowerShell as an experimental features (cannot find it in 7.2.2) or when will it come?
It's in the current 7.3.0 preview - PSNativeCommandErrorActionPreference
. After enabling the feature and restarting pwsh, try this:
whoami -xyzzy
get-date
& {
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true
whoami -xyzzy
get-date
}