dsccommunity / DscResource.Common

This module contains common functions that are used in DSC resources.
https://dsccommunity.org
MIT License
9 stars 9 forks source link

`New-InvalidOperationException`: Suggest adding a `PassThru` parameter #98

Closed johlju closed 7 months ago

johlju commented 1 year ago

Problem description

When using public commands we want to throw non-terminating errors using Write-Error, but also pass an exception or an error record to get the full stack trace.

The command New-InvalidOperationException would make it easy to create a InvalidOPerationException if it wouldn't always throw.

Verbose logs

n/a

How to reproduce

n/a

Expected behavior

New-InvalidOperationException should be able to return the InvalidOperation exception object.

Current behavior

ALways throws

Suggested solution

By adding a parameter PassThru it would return the exception object instead of throwing.

We could then do:

Write-Error -Exception (New-InvalidOperationException -Message 'My message' -PassThru)

We should also add a new New-ErrorRecord command (separate issue).

Operating system the target node is running

n/a

PowerShell version and build the target node is running

n/a

Module version used

n/a
johlju commented 1 year ago

Maybe a Get-InvalidOperationException that return an InvalidOperationException would be better than a PassThru parameter. New-InvalidOperationException could re-use a Get-InvalidOperationException, and New-ErrorRecord (proposed in issue #99) could have an Exception parameter that we could pass in with Get-InvalidOperationException. 🤔

johlju commented 8 months ago

Get-* suggest getting information that already exists somewhere, not creating new one. 🤔 Might be better to stick with New-* and using -PassThru that more suggest that we create something that will be returned on the pipeline.