Skip to content

Commit 586fd94

Browse files
Copilotjohlju
andcommitted
Remove assert-style commands from acceptable ThrowTerminatingError uses
Co-authored-by: johlju <[email protected]>
1 parent 70d34ca commit 586fd94

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

.github/instructions/dsc-community-style-guidelines-powershell.instructions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ applyTo: "{**/*.ps1,**/*.psm1,**/*.psd1}"
9898
- In catch blocks, pass original exception using `-Exception`
9999
- Always use `return` after `Write-Error` to avoid further processing
100100
- **Avoid `$PSCmdlet.ThrowTerminatingError()` in public commands** - creates command-terminating (not script-terminating) errors; callers must set `$ErrorActionPreference = 'Stop'` OR use try-catch (using `-ErrorAction 'Stop'` alone is insufficient)
101-
- Acceptable only in: assert-style commands, private functions, or state-changing catch blocks where operation failures must prevent further state changes
101+
- Acceptable only in: private functions, or state-changing catch blocks where operation failures must prevent further state changes
102+
- Assert-style commands should use `Write-Error` for standard behavior
102103
- Use `throw` only in `[ValidateScript()]` parameter validation attributes - it's the only valid mechanism there
103104
- .NET method exceptions (e.g., SMO methods) are always caught in try-catch blocks without needing to set `$ErrorActionPreference`
104105

CONTRIBUTING.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,16 @@ Use `$PSCmdlet.ThrowTerminatingError()` only in these limited scenarios:
412412

413413
1. **Private functions** that are only called internally and where the behavior
414414
is well understood
415-
2. **Assert-style commands** (like `Assert-SqlDscLogin`) whose purpose is to
416-
throw on failure
417-
3. **Commands with `SupportsShouldProcess`** where an operation failure in the
415+
2. **Commands with `SupportsShouldProcess`** where an operation failure in the
418416
`ShouldProcess` block must terminate (common for state-changing operations
419417
that cannot be partially completed)
420-
4. **Catch blocks in state-changing commands** where a critical operation
418+
3. **Catch blocks in state-changing commands** where a critical operation
421419
failure must be communicated as a terminating error to prevent further
422420
state changes
423421

422+
**Note:** Assert-style commands should use `Write-Error` instead - this provides
423+
standard PowerShell behavior where `-ErrorAction 'Stop'` alone works correctly.
424+
424425
In these cases, ensure callers are aware of the behavior. **Important:** When calling
425426
these commands, using `-ErrorAction 'Stop'` alone is NOT sufficient to stop the calling
426427
function. Callers must ALSO either:
@@ -616,7 +617,7 @@ $results = $items | Process-Items -ErrorAction 'Stop'
616617
| Blanket error handling for multiple cmdlets | Set `$ErrorActionPreference = 'Stop'` in try, restore in finally | Avoids adding `-ErrorAction 'Stop'` to each cmdlet; also catches ThrowTerminatingError from child commands |
617618
| Single cmdlet error handling | Use `-ErrorAction 'Stop'` on the cmdlet | Simpler and more explicit than setting `$ErrorActionPreference` |
618619
| Calling commands that use ThrowTerminatingError | Set `$ErrorActionPreference = 'Stop'` in caller OR wrap in try-catch | Using `-ErrorAction 'Stop'` alone is NOT sufficient; caller continues after error |
619-
| Assert-style commands | `$PSCmdlet.ThrowTerminatingError()` | Command purpose is to throw on failure |
620+
| Assert-style commands | `Write-Error` | Provides standard behavior; `-ErrorAction 'Stop'` alone works correctly for callers |
620621
| State-changing commands (catch blocks) | `$PSCmdlet.ThrowTerminatingError()` | Prevents partial state changes; caller must set `$ErrorActionPreference = 'Stop'` OR wrap in try-catch |
621622
| Private functions (internal use only) | `$PSCmdlet.ThrowTerminatingError()` or `Write-Error` | Behavior is understood by internal callers |
622623
| Parameter validation in `[ValidateScript()]` | `throw` | Only valid option within validation attributes |

0 commit comments

Comments
 (0)