Skip to content

Commit 903f6f0

Browse files
authored
Replace throw with ThrowTerminatingError in exception handling (#179)
1 parent 6094f9a commit 903f6f0

9 files changed

Lines changed: 54 additions & 17 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- New-*Exception
13+
- Use `ThrowTerminatingError` instead of `throw`. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177).
14+
1015
## [0.24.2] - 2025-08-27
1116

1217
### Changed

source/Private/Clear-ZeroedEnumPropertyValue.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function Clear-ZeroedEnumPropertyValue
2121
{
2222
[CmdletBinding()]
2323
[OutputType([System.Collections.Hashtable])]
24-
param (
24+
param
25+
(
2526
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
2627
[System.Collections.Hashtable]
2728
$InputObject

source/Public/Get-LocalizedDataForInvariantCulture.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,14 @@ function Get-LocalizedDataForInvariantCulture
132132

133133
if ([string]::IsNullOrEmpty($languageFile))
134134
{
135-
throw ($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder)
135+
$message = ($script:localizedData.Get_LocalizedDataForInvariantCulture_FileNotFoundInFolder -f ($localizedFileNamesToTry -join ','), $localizedFolder)
136+
$errorSplat = @{
137+
Exception = [System.IO.FileNotFoundException]::new($message)
138+
ErrorId = 'MachineStateIncorrect'
139+
ErrorCategory = [System.Management.Automation.ErrorCategory]::ObjectNotFound
140+
}
141+
142+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
136143
}
137144
else
138145
{
@@ -201,7 +208,7 @@ function Get-LocalizedDataForInvariantCulture
201208
}
202209
catch
203210
{
204-
throw $_
211+
$PSCmdlet.ThrowTerminatingError($_)
205212
}
206213

207214
# Check for non-terminating errors.

source/Public/New-InvalidDataException.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ function New-InvalidDataException
4242
ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
4343
}
4444

45-
throw (New-ErrorRecord @errorSplat)
45+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
4646
}

source/Public/New-InvalidOperationException.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
The error record containing the exception that is causing this terminating error.
1717
1818
.PARAMETER PassThru
19-
If specified, returns the error record instead of throwing it.
19+
If specified, returns the exception instead of throwing it.
2020
2121
.EXAMPLE
2222
try
@@ -60,19 +60,25 @@ function New-InvalidOperationException
6060

6161
if ($null -eq $ErrorRecord)
6262
{
63-
$invalidOperationException = [System.InvalidOperationException]::new($Message)
63+
$exception = [System.InvalidOperationException]::new($Message)
6464
}
6565
else
6666
{
67-
$invalidOperationException = [System.InvalidOperationException]::new($Message, $ErrorRecord.Exception)
67+
$exception = [System.InvalidOperationException]::new($Message, $ErrorRecord.Exception)
6868
}
6969

7070
if ($PassThru.IsPresent)
7171
{
72-
return $invalidOperationException
72+
return $exception
7373
}
7474
else
7575
{
76-
throw (New-ErrorRecord -Exception $invalidOperationException.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'InvalidOperation')
76+
$errorSplat = @{
77+
Exception = $exception.ToString()
78+
ErrorId = 'MachineStateIncorrect'
79+
ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation
80+
}
81+
82+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
7783
}
7884
}

source/Public/New-InvalidResultException.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ function New-InvalidResultException
5555

5656
$exception = New-Exception @PSBoundParameters
5757

58-
throw (New-ErrorRecord -Exception $exception.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'InvalidResult')
58+
$errorSplat = @{
59+
Exception = $exception.ToString()
60+
ErrorId = 'MachineStateIncorrect'
61+
ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidResult
62+
}
63+
64+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
5965
}

source/Public/New-NotImplementedException.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
The error record containing the exception that is causing this terminating error.
1313
1414
.PARAMETER PassThru
15-
If specified, returns the error record instead of throwing it.
15+
If specified, returns the exception instead of throwing it.
1616
1717
.OUTPUTS
1818
None
@@ -57,19 +57,25 @@ function New-NotImplementedException
5757

5858
if ($null -eq $ErrorRecord)
5959
{
60-
$notImplementedException = [System.NotImplementedException]::new($Message)
60+
$exception = [System.NotImplementedException]::new($Message)
6161
}
6262
else
6363
{
64-
$notImplementedException = [System.NotImplementedException]::new($Message, $ErrorRecord.Exception)
64+
$exception = [System.NotImplementedException]::new($Message, $ErrorRecord.Exception)
6565
}
6666

6767
if ($PassThru.IsPresent)
6868
{
69-
return $notImplementedException
69+
return $exception
7070
}
7171
else
7272
{
73-
throw (New-ErrorRecord -Exception $notImplementedException.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'NotImplemented')
73+
$errorSplat = @{
74+
Exception = $exception.ToString()
75+
ErrorId = 'MachineStateIncorrect'
76+
ErrorCategory = [System.Management.Automation.ErrorCategory]::NotImplemented
77+
}
78+
79+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
7480
}
7581
}

source/Public/New-ObjectNotFoundException.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ function New-ObjectNotFoundException
4949

5050
$exception = New-Exception @PSBoundParameters
5151

52-
throw (New-ErrorRecord -Exception $exception.ToString() -ErrorId 'MachineStateIncorrect' -ErrorCategory 'ObjectNotFound')
52+
$errorSplat = @{
53+
Exception = $exception.ToString()
54+
ErrorId = 'MachineStateIncorrect'
55+
ErrorCategory = [System.Management.Automation.ErrorCategory]::ObjectNotFound
56+
}
57+
58+
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
5359
}

tests/Unit/Public/New-InvalidDataException.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Describe 'New-InvalidDataException' {
5555
Should -Throw -PassThru
5656

5757
$exception.CategoryInfo.Category | Should -Be 'InvalidData'
58-
$exception.FullyQualifiedErrorId | Should -Be $mockErrorId
58+
$exception.FullyQualifiedErrorId | Should -BeLike ($mockErrorId + '*')
5959
$exception.Exception.Message | Should -Be $mockErrorMessage
6060
}
6161
}

0 commit comments

Comments
 (0)