-
Notifications
You must be signed in to change notification settings - Fork 228
Open
Labels
documentationThe issue is related to documentation only.The issue is related to documentation only.help wantedThe issue is up for grabs for anyone in the community.The issue is up for grabs for anyone in the community.
Description
The documentation should be updated according to this: #1966 (comment)
Also reference: https://stackoverflow.com/questions/49204918/difference-between-throw-and-pscmdlet-throwterminatingerror
Lines 368 to 379 in 3c30929
| When instead using `$PSCmdlet.ThrowTerminatingError()`: | |
| ```powershell | |
| $PSCmdlet.ThrowTerminatingError( | |
| [System.Management.Automation.ErrorRecord]::new( | |
| 'MyError', | |
| 'GS0001', | |
| [System.Management.Automation.ErrorCategory]::InvalidOperation, | |
| 'MyObjectOrValue' | |
| ) | |
| ) | |
| ``` |
I caught this today where I call a public command (or function) from a another public command. Example below.
function Get-Something
{
[CmdletBinding()]
param ()
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
'Error message',
'CODE',
[System.Management.Automation.ErrorCategory]::InvalidOperation,
'MyObject'
)
)
"Get-Something exiting" # CORRECT: Does not hit this
}
function Start-Something
{
[CmdletBinding()]
param ()
Get-Something -Name $null -ErrorAction 'Stop'
"Started" # BUG: This line is executed even though Get-Something throw an exception
}
# This hits the bug in Start-Something
Start-Something
# The user must add -ErrorAction 'Stop' to Start-Something to avoid the bug which is not intuitive
Start-Something -ErrorAction 'Stop'Similar but using Write-Error (non-terminating error):
function Get-Something
{
[CmdletBinding()]
param ()
Write-Error -Message 'Error message' -Category InvalidOperation -TargetObject 'MyObject' -ErrorId 'CODE'
"Get-Something exiting" # CORRECT: Does not hit this
}
function Start-Something
{
[CmdletBinding()]
param ()
Get-Something -Name $null -ErrorAction 'Stop'
"Started" # CORRECT: Does not hit this
}
# This works as expected, stops after the exception in Get-Something
Start-SomethingCopilot
Metadata
Metadata
Assignees
Labels
documentationThe issue is related to documentation only.The issue is related to documentation only.help wantedThe issue is up for grabs for anyone in the community.The issue is up for grabs for anyone in the community.