From cc94896e8094eb799b5d45960bd6df9160c37d0c Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:11:36 +0100 Subject: [PATCH 1/6] Remove throw usage --- .../Get-LocalizedDataForInvariantCulture.ps1 | 4 ++-- source/Public/New-InvalidDataException.ps1 | 2 +- source/Public/New-InvalidOperationException.ps1 | 14 ++++++++++---- source/Public/New-InvalidResultException.ps1 | 8 +++++++- source/Public/New-NotImplementedException.ps1 | 14 ++++++++++---- source/Public/New-ObjectNotFoundException.ps1 | 8 +++++++- .../Unit/Public/New-InvalidDataException.Tests.ps1 | 2 +- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/source/Public/Get-LocalizedDataForInvariantCulture.ps1 b/source/Public/Get-LocalizedDataForInvariantCulture.ps1 index 97724bd..366a6ff 100644 --- a/source/Public/Get-LocalizedDataForInvariantCulture.ps1 +++ b/source/Public/Get-LocalizedDataForInvariantCulture.ps1 @@ -132,7 +132,7 @@ function Get-LocalizedDataForInvariantCulture if ([string]::IsNullOrEmpty($languageFile)) { - throw ($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder) + $PSCmdlet.ThrowTerminatingError(($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder)) } else { @@ -201,7 +201,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..7ed19c2 100644 --- a/source/Public/New-InvalidOperationException.ps1 +++ b/source/Public/New-InvalidOperationException.ps1 @@ -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..3a858df 100644 --- a/source/Public/New-NotImplementedException.ps1 +++ b/source/Public/New-NotImplementedException.ps1 @@ -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 } } From 97ab0796fbc8bc2be279efd2223680d04bef2cc6 Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:11:41 +0100 Subject: [PATCH 2/6] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bca136..911fe34 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 From c4850551f910526ec32c87a7fc66e64d106d5f85 Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:30:14 +0100 Subject: [PATCH 3/6] construct correct error --- source/Public/Get-LocalizedDataForInvariantCulture.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/Public/Get-LocalizedDataForInvariantCulture.ps1 b/source/Public/Get-LocalizedDataForInvariantCulture.ps1 index 366a6ff..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)) { - $PSCmdlet.ThrowTerminatingError(($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 { From d5d239af1d331173aae1089c40095bb9b1c7123d Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:34:26 +0100 Subject: [PATCH 4/6] Improve changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 911fe34..8ae1cd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - New-*Exception - - Use ThrowTerminatingError instead of throw. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177). + - Use `ThrowTerminatingError` instead of `throw`. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177). ## [0.24.2] - 2025-08-27 From 19ac6f346cb2afc05d0debee058466f316d29fcd Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:34:42 +0100 Subject: [PATCH 5/6] Correct comment based help --- source/Public/New-InvalidOperationException.ps1 | 2 +- source/Public/New-NotImplementedException.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Public/New-InvalidOperationException.ps1 b/source/Public/New-InvalidOperationException.ps1 index 7ed19c2..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 diff --git a/source/Public/New-NotImplementedException.ps1 b/source/Public/New-NotImplementedException.ps1 index 3a858df..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 From 2859e05311c5c5125346f78003543ba135ff8ed1 Mon Sep 17 00:00:00 2001 From: Dan Hughes <2237515+dan-hughes@users.noreply.github.com> Date: Wed, 24 Sep 2025 17:11:57 +0100 Subject: [PATCH 6/6] Fix HQRM --- source/Private/Clear-ZeroedEnumPropertyValue.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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