dsccommunity / dsccommunity/SqlServerDsc
CONTRIBUTING: Update information around `$PSCmdlet.ThrowTerminatingError`
Open
Nobody has claimed this yet.
documentation
in progress
- Dominant language
- PowerShell
- Stars
- 385
- Forks
- 224
- PR merge metrics
- No merged PRs in 30d
Description
The documentation should be updated according to this: https://github.com/dsccommunity/SqlServerDsc/pull/1966#issuecomment-1703776151
Also reference: https://stackoverflow.com/questions/49204918/difference-between-throw-and-pscmdlet-throwterminatingerror
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-Something
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.