Skip to content

CONTRIBUTING: Simplify error handling guidelines - use Write-Error - #2364

Open
johlju with Copilot wants to merge 16 commits into
mainfrom
copilot/update-pscmdlet-throwterminatingerror
Open

CONTRIBUTING: Simplify error handling guidelines - use Write-Error#2364
johlju with Copilot wants to merge 16 commits into
mainfrom
copilot/update-pscmdlet-throwterminatingerror

Conversation

Copilot AI commented Dec 7, 2025

Copy link
Copy Markdown
Contributor
  • Update AI instructions: clarify return only needed for non-terminating Write-Error
  • Update CONTRIBUTING.md: clarify return usage and remove it from -ErrorAction Stop example
  • Fix markdown table formatting: remove leading/trailing pipes to satisfy MD055
  • All code review feedback addressed
Original prompt

This section details on the original issue you should resolve

<issue_title>CONTRIBUTING: Update information around $PSCmdlet.ThrowTerminatingError</issue_title>
<issue_description>The documentation should be updated according to this: #1966 (comment)

Also reference: https://stackoverflow.com/questions/49204918/difference-between-throw-and-pscmdlet-throwterminatingerror

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-Something
```</issue_description>

<agent_instructions>Read the issue and comment, follow the links and read those comments and links. Based on that make a conclusion about how to best use different ways of throwing a terminating and non-terminating error in public commands and update CONTRIBUTING.md. We should never use `throw` in public commands though, so that is not in scope. My basic understanding is that we should use `Write-Error` whenever possible, but should we limit or entirely not use `$PSCmdlet.ThrowTerminatingError()`? What scenarios should we use each in, and if possible give examples of scenarios where each option fail and where they succeed. We have scenarios like commands accepting pipeline input (that process several items at a time over pipeline), not accepting pipeline input, commands that should never throw a terminating error by default but a non-terminating error and commands that in certain code paths must throw a terminating error.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


This change is Reviewable

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review The pull request needs a code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CONTRIBUTING: Update information around $PSCmdlet.ThrowTerminatingError

2 participants