Skip to content

-ErrorAction:Stop doesn't work as it should under special conditions. #12848

Closed
@ZonderP

Description

@ZonderP

Since I yesterday reported a different issue with a simple example, I though it would be a good time to finally also report a bug that really annoys me, since it somehow makes weeks of work useless.

Over the last years I spent many days (or even weeks - all in all) on working on something I call colored HTML transcription (and as a side project 'ANSI colored output' - for use in Jenkins).
Colored HTML transcription mainly works like 'Start-Transcript'/'Stop-Transcript' but instead of just logging text, it more or less transfers the console to an html file with all the colors we see on the console.
For this to work as expected I override all the 'Write-' functions (e.g.: 'Write-Information', 'Write-Warning', 'Write-Error' - except 'Write-Output'.)

I write cmdlets in my modules usually in a way, that they use 'Write-Error', when I consider something to be an error, thus giving the users the possibility to decide for themselves, if those errors should be terminating or not by calling them either with '-ErrorAction:Stop' or not so.

My cmdlets in my modules serve mainly for automated testing, so it is often desired, that a test fails, if an error occurs and thus many of those cmdlets are called with '-ErrorAction:Stop'.
And for automated testing we used to use 'Colored HTML transcription', because for human testers it is very convenient to see colors in transcript files which got produced for test suites which run hundreds of tests over night to immediately see errors and warnings and hints.
But we had to stop to make use of this nice feature, because at some point I found out, that the very important feature of 'auto terminating' a single test with 'Failed' in case of an error didn't work under some circumstances, because of the bug, where -ErrorAction:Stop' just doesn't work.

As a side note:
The bug is easily reproducible on 7.0.1 as well as on 5.1.

Steps to reproduce

Overwrite 'Write-Error' in a module the way it can be found in many examples on the Internet and I think also in the 'Windows PowerShell Cookbook' by 'O'Reilly' (steppablePipeline).
The overwrite just may use again the original 'Microsoft.PowerShell.Utility\Write-Error'.
In that same module write a simple cmdlet called like 'Test-WriteErrorInOverloadModule' which does nothing else then call 'Write-Error' with some message.
Then in some other module write a simple cmdlet called like 'Test-WriteErrorInOtherModule' which also just calls 'Write-Error' with some message.

In the module where the 'Write-Error' overwrite is defined, also add a function which replaces the original 'Write-Error' cmdlet with the 'Overwrite'.

Now call the function which does the 'Write-Error' replacement and then call:
'Test-WriteErrorInOverloadModule -ErrorAction:Stop' within a try/catch
and then call 'Test-WriteErrorInOtherModule -ErrorAction:Stop' withing a try/catch and see what happens!
The first one will go to the catch block, but the second one not!

Expected behavior

Also the call to 'Test-WriteErrorInOtherModule -ErrorAction:Stop' should go to the catch block.

Actual behavior

The call to 'Test-WriteErrorInOtherModule -ErrorAction:Stop' doesn't make the error thrown within that function to be a terminating error as expected.

Environment data

Name Value


PSVersion 7.0.1
PSEdition Core
GitCommitId 7.0.1
OS Microsoft Windows 6.1.7601 Service Pack 1
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

I'm quite sure, that some guys here are aware of that problem, because when I tried to find a solution for it back then some months ago - I stumbled over dozens of Powershell 'Error-Handling' related issues and really very long discussions on this topic here on this Powershell GitHub where I also found indications, that this is a real problem that - depending from where called, the behavior might be different.
If you ask me - this is a severe issue, because if you cannot rely on something that important to work the same way independently of some preconditions, you really loose a lot of trust.

Since this bug is not that easy to reproduce, I attached a zip file with 2 modules where everything already is prepared, so that you do not have to write those before-mentioned cmdlets yourself.
Just call the script 'Check.ps1' (located in the 'WriteErrorOverloadModule' base directory) and see what happens.

If someone would know of a workaround to overcome this problem, I would very much appreciate if you could give me hints how to do so.

PoSh Example for Write-Error overload problem.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions