question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set $ErrorActionPreference to Stop in PowerShell function templates code

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JustinGrotecommented, Nov 18, 2019

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)

0reactions
Francisco-Gaminocommented, Aug 2, 2023

This needs to be validated with different version of PowerShell.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hey, Scripting Guy! How Can I Use $ErrorActionPreference ...
If the value of $ErrorActionPreference is set to Stop, a script will halt execution at the failed command–-even if the subsequent commands would ......
Read more >
Chapter 11. A template for handling and reporting errors
Preference variables are a set of built-in PowerShell variables that allow you to customize the shell's behavior. You use the $ErrorActionPreference variable to ......
Read more >
A warning on $ErrorActionPreference and Try/Catch for .NET ...
I recently stumbled upon a PowerShell Script from a . ... set error action preference so errors don't stop and the trycatch #...
Read more >
How to use the ErrorActionPreference variable in PowerShell
ErrorActionPreference variable in PowerShell is to control the non-terminating errors by converting them to terminating errors.
Read more >
PowerShell script template
I put all my sub functions in the "BEGIN" section. My script code goes in the "try" block. Source Code. This script has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found