PowerShellPracticeAndStyle icon indicating copy to clipboard operation
PowerShellPracticeAndStyle copied to clipboard

ERR-01 and ERR-02 implies that you know all commands

Open paalbra opened this issue 9 years ago • 0 comments
trafficstars

In ERR-01 and ERR-02 you're told to use -ErrorAction on all cmdlets and set ErrorActionPreference around everything else, where needed.

This leaves it to the scripter to figure out how every command emits its errors and how they're emitted. I believe this is impossible.

Personally I put this in the first two lines of the begin block in every advanced function I create:

Set-StrictMode -Version "Latest"
Set-Variable -Name "ErrorActionPreference" -Scope "Script" -Value "Stop"

With these two lines you will fail fast, hard and consistently (the users ErrorActionPreference will not matter).

I write Python quite often and would like to quote parts of The Zen of Python:

Errors should never pass silently. Unless explicitly silenced.

I believe any PowerShell script also should behave this way. Silent errors can and will lead to nothing but confusion and frustrating debug situations.

paalbra avatar Nov 08 '16 08:11 paalbra