Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion source/Private/Clear-ZeroedEnumPropertyValue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function Clear-ZeroedEnumPropertyValue
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param (
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Collections.Hashtable]
$InputObject
Expand Down
11 changes: 9 additions & 2 deletions source/Public/Get-LocalizedDataForInvariantCulture.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -201,7 +208,7 @@ function Get-LocalizedDataForInvariantCulture
}
catch
{
throw $_
$PSCmdlet.ThrowTerminatingError($_)
}

# Check for non-terminating errors.
Expand Down
2 changes: 1 addition & 1 deletion source/Public/New-InvalidDataException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ function New-InvalidDataException
ErrorCategory = [System.Management.Automation.ErrorCategory]::InvalidData
}

throw (New-ErrorRecord @errorSplat)
$PSCmdlet.ThrowTerminatingError((New-ErrorRecord @errorSplat))
}
16 changes: 11 additions & 5 deletions source/Public/New-InvalidOperationException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Comment thread
dan-hughes marked this conversation as resolved.
}
8 changes: 7 additions & 1 deletion source/Public/New-InvalidResultException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Comment thread
dan-hughes marked this conversation as resolved.
16 changes: 11 additions & 5 deletions source/Public/New-NotImplementedException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Comment thread
dan-hughes marked this conversation as resolved.
}
8 changes: 7 additions & 1 deletion source/Public/New-ObjectNotFoundException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Comment thread
dan-hughes marked this conversation as resolved.
2 changes: 1 addition & 1 deletion tests/Unit/Public/New-InvalidDataException.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down