How should a PowerShell module document whether errors terminate or continue?

0
6
Asked By MellowBirch42 On

I maintain a PowerShell module with roughly 40 exported provisioning functions. After auditing 127 error paths, I found four different behaviors: 52 used throw with a string, 34 used Write-Error followed by return, 23 used $PSCmdlet.ThrowTerminatingError with an ErrorRecord, 11 silently returned after a failed condition, and 7 used throw with a constructed ErrorRecord.

This affected callers in practical ways. Write-Error produces a non-terminating error, while throw and ThrowTerminatingError can stop execution at different levels. As a result, a caller using try/catch would only reliably catch some of the failures. I standardized validation failures on ThrowTerminatingError, used non-terminating errors for recoverable per-item problems, and converted silent returns into proper error records. The module now has two intentional patterns instead of four, with behavior based on whether the function is an advanced function.

The remaining issue is documentation. CmdletBinding, OutputType, and comment-based help do not appear to provide a formal way to declare whether a function terminates on failure or writes a non-terminating error. How do people who publish PowerShell modules communicate this error-handling contract to consumers?

3 Answers

Answered By QuietNova53 On

Using Write-Error followed by return is not always necessary. You can emit a non-terminating error with an appropriate category and let the caller choose the action through -ErrorAction or ErrorActionPreference. That matters for pipeline commands: stopping on the first bad input and continuing past bad inputs are both useful behaviors, depending on whether the failure affects the entire operation or just one object.

For public modules, I would document the error category, whether processing continues for later pipeline inputs, and whether callers can promote the error with -ErrorAction Stop. A small behavior table in the help text or module documentation can be clearer than relying on implementation details.

Answered By CobaltLark7 On

There is no built-in declaration that cleanly exposes this contract. The practical approach is to document it explicitly in comment-based help, especially the NOTES section, and describe which conditions are terminating versus non-terminating. Consumers often still need to inspect the implementation or test behavior, which is a genuine weakness in PowerShell's error model.

As a general convention, commands tend to use terminating behavior when the whole operation cannot continue, and non-terminating errors when only one input object is affected. That convention helps, but it is not perfectly consistent, so documenting the module's own rules is worthwhile.

Answered By RiverMoss19 On

For non-terminating errors in an advanced function, prefer $PSCmdlet.WriteError() when you already have an ErrorRecord rather than relying on Write-Error. It gives you more direct control over the record and avoids some surprising interaction with automatic success-state variables.

Also make sure the function honors the common -ErrorAction behavior. A caller should be able to promote a recoverable error with -ErrorAction Stop and catch it with try/catch. Hard-coding every failure as throw can make per-input pipeline processing unnecessarily brittle.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.