diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bca136..8ae1cd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- New-*Exception + - Use `ThrowTerminatingError` instead of `throw`. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177). + ## [0.24.2] - 2025-08-27 ### Changed diff --git a/source/Private/Clear-ZeroedEnumPropertyValue.ps1 b/source/Private/Clear-ZeroedEnumPropertyValue.ps1 index 95a9d63..0209889 100644 --- a/source/Private/Clear-ZeroedEnumPropertyValue.ps1 +++ b/source/Private/Clear-ZeroedEnumPropertyValue.ps1 @@ -21,7 +21,8 @@ function Clear-ZeroedEnumPropertyValue { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] - param ( + param + ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [System.Collections.Hashtable] $InputObject diff --git a/source/Public/Get-LocalizedDataForInvariantCulture.ps1 b/source/Public/Get-LocalizedDataForInvariantCulture.ps1 index 97724bd..88e2a73 100644 --- a/source/Public/Get-LocalizedDataForInvariantCulture.ps1 +++ b/source/Public/Get-LocalizedDataForInvariantCulture.ps1 @@ -132,7 +132,14 @@ function Get-LocalizedDataForInvariantCulture if ([string]::IsNullOrEmpty($languageFile)) { - throw ($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder) + $message = ($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder) + $errorSplat = @{ + Exception = [System.IO.FileNotFoundException]::new($message) + ErrorId = 'MachineStateIncorrect' + ErrorCategory = [System.Management.Automation.ErrorCategory]::ObjectNotFound + } + + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } else { @@ -201,7 +208,7 @@ function Get-LocalizedDataForInvariantCulture } catch { - throw $_ + $PSCmdlet.ThrowTerminatingError($_) } # Check for non-terminating errors. diff --git a/source/Public/New-InvalidDataException.ps1 b/source/Public/New-InvalidDataException.ps1 index a89f53c..d996084 100644 --- a/source/Public/New-InvalidDataException.ps1 +++ b/source/Public/New-InvalidDataException.ps1 @@ -42,5 +42,5 @@ function New-InvalidDataException ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidData } - throw (New-ErrorRecord @errorSplat) + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } diff --git a/source/Public/New-InvalidOperationException.ps1 b/source/Public/New-InvalidOperationException.ps1 index 8cb3ea6..15103eb 100644 --- a/source/Public/New-InvalidOperationException.ps1 +++ b/source/Public/New-InvalidOperationException.ps1 @@ -16,7 +16,7 @@ The error record containing the exception that is causing this terminating error. .PARAMETER PassThru - If specified, returns the error record instead of throwing it. + If specified, returns the exception instead of throwing it. .EXAMPLE try @@ -60,19 +60,25 @@ function New-InvalidOperationException if ($null -eq $ErrorRecord) { - $invalidOperationException = [System.InvalidOperationException]::new($Message) + $exception = [System.InvalidOperationException]::new($Message) } else { - $invalidOperationException = [System.InvalidOperationException]::new($Message, $ErrorRecord.Exception) + $exception = [System.InvalidOperationException]::new($Message, $ErrorRecord.Exception) } if ($PassThru.IsPresent) { - return $invalidOperationException + return $exception } else { - throw (New-ErrorRecord -Exception $invalidOperationException.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'InvalidOperation') + $errorSplat = @{ + Exception = $exception.ToString() + ErrorId = 'MachineStateIncorrect' + ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation + } + + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } } diff --git a/source/Public/New-InvalidResultException.ps1 b/source/Public/New-InvalidResultException.ps1 index 063d66b..0ca6192 100644 --- a/source/Public/New-InvalidResultException.ps1 +++ b/source/Public/New-InvalidResultException.ps1 @@ -55,5 +55,11 @@ function New-InvalidResultException $exception = New-Exception @PSBoundParameters - throw (New-ErrorRecord -Exception $exception.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'InvalidResult') + $errorSplat = @{ + Exception = $exception.ToString() + ErrorId = 'MachineStateIncorrect' + ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidResult + } + + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } diff --git a/source/Public/New-NotImplementedException.ps1 b/source/Public/New-NotImplementedException.ps1 index c2cad61..ed092c0 100644 --- a/source/Public/New-NotImplementedException.ps1 +++ b/source/Public/New-NotImplementedException.ps1 @@ -12,7 +12,7 @@ The error record containing the exception that is causing this terminating error. .PARAMETER PassThru - If specified, returns the error record instead of throwing it. + If specified, returns the exception instead of throwing it. .OUTPUTS None @@ -57,19 +57,25 @@ function New-NotImplementedException if ($null -eq $ErrorRecord) { - $notImplementedException = [System.NotImplementedException]::new($Message) + $exception = [System.NotImplementedException]::new($Message) } else { - $notImplementedException = [System.NotImplementedException]::new($Message, $ErrorRecord.Exception) + $exception = [System.NotImplementedException]::new($Message, $ErrorRecord.Exception) } if ($PassThru.IsPresent) { - return $notImplementedException + return $exception } else { - throw (New-ErrorRecord -Exception $notImplementedException.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'NotImplemented') + $errorSplat = @{ + Exception = $exception.ToString() + ErrorId = 'MachineStateIncorrect' + ErrorCategory = [System.Management.Automation.ErrorCategory]::NotImplemented + } + + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } } diff --git a/source/Public/New-ObjectNotFoundException.ps1 b/source/Public/New-ObjectNotFoundException.ps1 index 11fd047..402f9a7 100644 --- a/source/Public/New-ObjectNotFoundException.ps1 +++ b/source/Public/New-ObjectNotFoundException.ps1 @@ -49,5 +49,11 @@ function New-ObjectNotFoundException $exception = New-Exception @PSBoundParameters - throw (New-ErrorRecord -Exception $exception.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'ObjectNotFound') + $errorSplat = @{ + Exception = $exception.ToString() + ErrorId = 'MachineStateIncorrect' + ErrorCategory = [System.Management.Automation.ErrorCategory]::ObjectNotFound + } + + $PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat)) } diff --git a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 index df21a93..74c0c62 100644 --- a/tests/Unit/Public/New-InvalidDataException.Tests.ps1 +++ b/tests/Unit/Public/New-InvalidDataException.Tests.ps1 @@ -55,7 +55,7 @@ Describe 'New-InvalidDataException' { Should -Throw -PassThru $exception.CategoryInfo.Category | Should -Be 'InvalidData' - $exception.FullyQualifiedErrorId | Should -Be $mockErrorId + $exception.FullyQualifiedErrorId | Should -BeLike ($mockErrorId + '*') $exception.Exception.Message | Should -Be $mockErrorMessage } }