Skip to content

Commit

Permalink
Add New-Exception, and PassThru parameter to exception commands (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jan 23, 2024
1 parent 1ada77f commit f06681b
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 63 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
that can copy from omne target to the other ([issue #102](https://github.com/dsccommunity/DscResource.Common/issues/102)).
- A new parameter `PassThru` that, if specified, returns the path that
was set.
- `New-Exception`
- New command that creates and returns an `[System.Exception]`.
- `New-ArgumentException`
- Now takes a parameter `PassThru` that returns the error record that was
created (and does not throw).
- `New-InvalidOperationException`
- Now takes a parameter `PassThru` that returns the error record that was
created (and does not throw) ([issue #98](https://github.com/dsccommunity/DscResource.Common/issues/98)).
- `New-InvalidResultException`
- Now takes a parameter `PassThru` that returns the error record that was
created (and does not throw).
- `New-NotImplementedException`
- Now takes a parameter `PassThru` that returns the error record that was
created (and does not throw).

### Changed

Expand All @@ -23,20 +37,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change the word cmdlet to command throughout in the documentation, code
and localization strings.
- A meta task now removes the built module from the session if it is imported.
- Wiki source file HOME was modified to not link to README for help after
command documentation now is in the wiki.
- `Get-LocalizedData`
- Refactored to simplify execution and debugging. The command previously
used a steppable pipeline (proxies `Import-LocalizedData`), that was
removed since it was not possible to use the command in a pipeline.
It just made it more complex and harder to debug. There are more
debug messages added to hopefully simplify solving some hard to find
edge cases bugs.
- `New-ArgumentException`
- Now has a command alias `New-InvalidArgumentException` and the command
was renamed to match the exception name.
- `New-InvalidDataException`
- The parameter `Message` has a parameter alias `ErrorMessage` to make
the command have the same parameter names as the other `New-*Exception`
commands.

### Fixed

- `Assert-BoundParameter`
- Fixed example in documentation that were referencing an invalid command name.
- `Get-LocalizedData`
- One debug message was wrongly using a format operator ([issue #111](https://github.com/dsccommunity/DscResource.Common/issues/111).
- `New-ObjectNotFoundException`
- Updated typo in comment-based help.

## [0.16.0] - 2023-04-10

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
<#
.SYNOPSIS
Creates and throws an invalid argument exception.
Creates and throws or returns an invalid argument exception.
.DESCRIPTION
Creates and throws an invalid argument exception.
Creates and throws or returns an invalid argument exception.
.PARAMETER Message
The message explaining why this error is being thrown.
.PARAMETER ArgumentName
The name of the invalid argument that is causing this error to be thrown.
.PARAMETER PassThru
If specified, returns the error record instead of throwing it.
.OUTPUTS
None
System.Management.Automation.ErrorRecord
.EXAMPLE
New-InvalidArgumentException -ArgumentName 'Action' -Message 'My error message'
New-ArgumentException -ArgumentName 'Action' -Message 'My error message'
Creates and throws an invalid argument exception for (parameter) 'Action'
with the message 'My error message'.
.EXAMPLE
$errorRecord = New-ArgumentException -ArgumentName 'Action' -Message 'My error message' -PassThru
Creates an invalid argument exception for (parameter) 'Action'
with the message 'My error message' and returns the exception.
#>
function New-InvalidArgumentException

function New-ArgumentException
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[CmdletBinding()]
[Alias('New-InvalidArgumentException')]
param
(
[Parameter(Mandatory = $true)]
Expand All @@ -34,7 +46,11 @@ function New-InvalidArgumentException
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$ArgumentName
$ArgumentName,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$PassThru
)

$argumentException = New-Object -TypeName 'ArgumentException' `
Expand All @@ -47,5 +63,12 @@ function New-InvalidArgumentException

$errorRecord = New-Object @newObjectParameters

throw $errorRecord
if ($PassThru.IsPresent)
{
return $argumentException
}
else
{
throw $errorRecord
}
}
69 changes: 69 additions & 0 deletions source/Public/New-Exception.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<#
.SYNOPSIS
Creates and returns an exception.
.DESCRIPTION
Creates an exception that will be returned.
.OUTPUTS
None
.PARAMETER Message
The message explaining why this error is being thrown.
.PARAMETER ErrorRecord
The error record containing the exception that is causing this terminating error.
.OUTPUTS
System.Exception
.EXAMPLE
$errorRecord = New-Exception -Message 'An error occurred'
Creates and returns an exception with the message 'An error occurred'.
.EXAMPLE
try
{
Get-ChildItem -Path $path -ErrorAction 'Stop'
}
catch
{
$exception = New-Exception -Message 'Could not get files' -ErrorRecord $_
}
Returns an exception with the message 'Could not get files' and includes
the exception that caused this terminating error.
#>
function New-Exception
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[CmdletBinding()]
[OutputType([System.Exception])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,

[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
)

if ($null -eq $ErrorRecord)
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message)
}
else
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message, $ErrorRecord.Exception)
}

return $exception
}
15 changes: 9 additions & 6 deletions source/Public/New-InvalidDataException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
.PARAMETER ErrorId
The error Id to assign to the exception.
.PARAMETER ErrorMessage
.PARAMETER Message
The error message to assign to the exception.
.EXAMPLE
New-InvalidDataException -ErrorId 'InvalidDataError' -ErrorMessage 'My error message'
New-InvalidDataException -ErrorId 'InvalidDataError' -Message 'My error message'
Creates and throws an invalid data exception with the error id 'InvalidDataError'
and with the message 'My error message'.
Expand All @@ -31,16 +31,19 @@ function New-InvalidDataException
$ErrorId,

[Parameter(Mandatory = $true)]
[Alias('ErrorMessage')]
[System.String]
$ErrorMessage
$Message
)

$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData

$exception = New-Object `
-TypeName System.InvalidOperationException `
-ArgumentList $ErrorMessage
-TypeName 'System.InvalidOperationException' `
-ArgumentList $Message

$errorRecord = New-Object `
-TypeName System.Management.Automation.ErrorRecord `
-TypeName 'System.Management.Automation.ErrorRecord' `
-ArgumentList $exception, $ErrorId, $errorCategory, $null

throw $errorRecord
Expand Down
39 changes: 30 additions & 9 deletions source/Public/New-InvalidOperationException.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<#
.SYNOPSIS
Creates and throws an invalid operation exception.
Creates and throws or returns an invalid operation exception.
.DESCRIPTION
Creates and throws an invalid operation exception.
Creates and throws or returns an invalid operation exception.
.OUTPUTS
None
None. If the PassThru parameter is not specified the command throws an error record.
System.InvalidOperationException. If the PassThru parameter is specified the command returns an error record.
.PARAMETER Message
The message explaining why this error is being thrown.
.PARAMETER ErrorRecord
The error record containing the exception that is causing this terminating error.
.PARAMETER PassThru
If specified, returns the error record instead of throwing it.
.EXAMPLE
try
{
Expand All @@ -26,11 +30,17 @@
Creates and throws an invalid operation exception with the message 'My error message'
and includes the exception that caused this terminating error.
#>
.EXAMPLE
$errorRecord = New-InvalidOperationException -Message 'My error message' -PassThru
Creates and returns an invalid operation exception with the message 'My error message'.
#>
function New-InvalidOperationException
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[CmdletBinding()]
[OutputType([System.InvalidOperationException])]
param
(
[Parameter(Mandatory = $true)]
Expand All @@ -41,17 +51,21 @@ function New-InvalidOperationException
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
$ErrorRecord,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$PassThru
)

if ($null -eq $ErrorRecord)
{
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
$invalidOperationException = New-Object -TypeName 'System.InvalidOperationException' `
-ArgumentList @($Message)
}
else
{
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
$invalidOperationException = New-Object -TypeName 'System.InvalidOperationException' `
-ArgumentList @($Message, $ErrorRecord.Exception)
}

Expand All @@ -65,7 +79,14 @@ function New-InvalidOperationException
)
}

$errorRecordToThrow = New-Object @newObjectParameters
$errorRecordToReturn = New-Object @newObjectParameters

throw $errorRecordToThrow
if ($PassThru.IsPresent)
{
return $invalidOperationException
}
else
{
throw $errorRecordToReturn
}
}
11 changes: 1 addition & 10 deletions source/Public/New-InvalidResultException.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ function New-InvalidResultException
$ErrorRecord
)

if ($null -eq $ErrorRecord)
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message)
}
else
{
$exception = New-Object -TypeName 'System.Exception' `
-ArgumentList @($Message, $ErrorRecord.Exception)
}
$exception = New-Exception @PSBoundParameters

$newObjectParameters = @{
TypeName = 'System.Management.Automation.ErrorRecord'
Expand Down
Loading

0 comments on commit f06681b

Please sign in to comment.