Set $ErrorActionPreference to Stop in PowerShell function templates code
See original GitHub issueProblem
By PowerShell design, the default value of $ErrorActionPreference
is 'Continue'
. New PowerShell users who are not familiar with the concept of non-terminating errors often get confused by the result: the PowerShell function execution seems to be completed successfully but has not in fact performed what it was supposed to perform, and the only way to notice this is to look at the logs.
This problem occasionally affects even experienced PowerShell users, and it seems to be a frequent annoyance for beginners, so it is worth mitigating. At the same time, we don’t want to surprise experienced PowerShell users with by modifying the default behavior or introducing any other breaking changes, and we want to avoid creating Functions-specific abstractions on top of PowerShell.
Proposal
When a new PowerShell function is added from a template, the auto-generated code should include the following:
# By default, stop on any error
# (for more details, see <link to the doc, e.g. https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_preference_variables#erroractionpreference>)
$ErrorActionPreference = 'Stop'
As a result, this will make users aware of the difference between terminating vs. non-terminating errors, invite them to learn more if interested, and set a more intuitively expected behavior. At the same time, users familiar with PowerShell who insist on any different behavior will have an opportunity to review, change, or delete this code.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Makes sense to me and wouldn’t be a breaking change unless someone has some edge case where they are doing code generation off the existing templates and expecting a certain behavior (pretty unlikely)
This needs to be validated with different version of PowerShell.