Skip to content

Set $ErrorActionPreference to Stop in PowerShell function templates code #364

@AnatoliB

Description

@AnatoliB

Problem

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions