Skip to content

Commit 92489fc

Browse files
Copilotjohlju
andcommitted
Clarify return usage with Write-Error and fix markdown table formatting
Co-authored-by: johlju <[email protected]>
1 parent 026f90f commit 92489fc

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ applyTo: "{**/*.ps1,**/*.psm1,**/*.psd1}"
9696
- For non-terminating errors: Omit `-ErrorAction` parameter (caller controls via `-ErrorAction`)
9797
- Always include `-Message` (localized string), `-Category` (relevant error category), `-ErrorId` (unique ID matching localized string ID), `-TargetObject` (object causing error)
9898
- In catch blocks, pass original exception using `-Exception`
99-
- Always use `return` after `Write-Error` to avoid further processing
99+
- Use `return` only after non-terminating `Write-Error` to stop further processing. Omit `return` when using `-ErrorAction 'Stop'`.
100100
- **Never use `$PSCmdlet.ThrowTerminatingError()` in public commands** - it creates command-terminating (not script-terminating) errors; use `Write-Error` with `-ErrorAction 'Stop'` instead
101101
- May be used in private functions where behavior is understood by internal callers
102102
- **Never use `throw` in public commands** except in `[ValidateScript()]` parameter validation attributes (it's the only valid mechanism there)

CONTRIBUTING.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ $db = Get-Database -Name 'NonExistent' -ErrorAction 'Stop'
353353
```
354354

355355
> [!IMPORTANT]
356-
> Always use `return` after `Write-Error` to prevent further processing in the
357-
> command. Without `return`, the command continues executing, which may cause
358-
> unexpected behavior or additional errors.
356+
> Use `return` after `Write-Error` only for non-terminating errors (no `-ErrorAction 'Stop'`)
357+
> to stop further processing in the current function. Omit `return` when using
358+
> `-ErrorAction 'Stop'`, as execution stops automatically.
359359
360360
##### Error Handling in Public Commands
361361

@@ -374,8 +374,6 @@ function Get-Database
374374
-ErrorId 'GD0001' `
375375
-TargetObject $Name `
376376
-ErrorAction 'Stop'
377-
378-
return
379377
}
380378
381379
# Continue processing...
@@ -507,14 +505,12 @@ $results = $items | Process-Items -ErrorAction 'Stop'
507505

508506
##### Summary
509507

510-
<!-- markdownlint-disable MD013 - Line length -->
511508
| Scenario | Use | Notes |
512-
|----------|-----|-------|
509+
| --- | --- | --- |
513510
| Terminating errors in public commands | `Write-Error` with `-ErrorAction 'Stop'` | Simple and standard PowerShell behavior |
514511
| Non-terminating errors in public commands | `Write-Error` without `-ErrorAction` | Allows caller to control termination via `-ErrorAction` |
515512
| Pipeline processing with multiple items | `Write-Error` without `-ErrorAction` | Allows processing to continue for remaining items |
516513
| Catching .NET method exceptions | try-catch with `Write-Error` | .NET exceptions are always caught automatically |
517514
| Parameter validation in `[ValidateScript()]` | `throw` | Only valid option within validation attributes |
518515
| Private functions (internal use only) | `Write-Error` or `$PSCmdlet.ThrowTerminatingError()` | Behavior is understood by internal callers |
519516
| Public commands | Never use `throw` or `$PSCmdlet.ThrowTerminatingError()` | Use `Write-Error` instead |
520-
<!-- markdownlint-enable MD013 - Line length -->

0 commit comments

Comments
 (0)