-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new command New-ErrorRecord (#118)
- Loading branch information
Showing
9 changed files
with
232 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<# | ||
.SYNOPSIS | ||
Creates a new ErrorRecord. | ||
.DESCRIPTION | ||
The New-ErrorRecord function creates a new ErrorRecord with the specified parameters. | ||
.PARAMETER ErrorRecord | ||
Specifies an existing ErrorRecord. | ||
.PARAMETER Exception | ||
Specifies the exception that caused the error. | ||
If an error record is passed to parameter ErrorRecord and if the wrapped exception | ||
in the error record contains a `[System.Management.Automation.ParentContainsErrorRecordException]`, | ||
the new ErrorRecord should have this exception as its Exception instead. | ||
.PARAMETER ErrorCategory | ||
Specifies the category of the error. | ||
.PARAMETER TargetObject | ||
Specifies the object that was being manipulated when the error occurred. | ||
.PARAMETER ErrorId | ||
Specifies a string that uniquely identifies the error. | ||
.EXAMPLE | ||
$ex = New-Exception -Message 'An error occurred.' | ||
$errorRecord = New-ErrorRecord -Exception $ex -ErrorCategory 'InvalidOperation' | ||
This example creates a new ErrorRecord with the specified parameters. Passing | ||
'InvalidOperation' which is one available value of the enum `[System.Management.Automation.ErrorCategory]`. | ||
.EXAMPLE | ||
$ex = New-Exception -Message 'An error occurred.' | ||
$errorRecord = New-ErrorRecord -Exception $ex -ErrorCategory 'InvalidOperation' -TargetObject $myObject | ||
This example creates a new ErrorRecord with the specified parameters. TargetObject | ||
is set to the object that was being manipulated when the error occurred. | ||
.EXAMPLE | ||
$ex = New-Exception -Message 'An error occurred.' | ||
$errorRecord = New-ErrorRecord -Exception $ex -ErrorCategory 'InvalidOperation' -ErrorId 'MyErrorId' | ||
This example creates a new ErrorRecord with the specified parameters. Passing | ||
ErrorId that will be set as the FullyQualifiedErrorId in the error record. | ||
.EXAMPLE | ||
$existingErrorRecord = [System.Management.Automation.ErrorRecord]::new( | ||
[System.Management.Automation.ParentContainsErrorRecordException]::new('Existing error'), | ||
'ExistingErrorId', | ||
[System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
$null | ||
) | ||
$newException = [System.Exception]::new('New error') | ||
$newErrorRecord = New-ErrorRecord -ErrorRecord $existingErrorRecord -Exception $newException | ||
$newErrorRecord.Exception.Message | ||
This example first creates an emulated ErrorRecord that contain a `ParentContainsErrorRecordException` | ||
which will be replaced by the new exception passed to New-ErrorRecord. The | ||
result of `$newErrorRecord.Exception.Message` will be 'New error'. | ||
.INPUTS | ||
System.Management.Automation.ErrorRecord, System.Exception, System.Management.Automation.ErrorCategory, System.Object, System.String | ||
.OUTPUTS | ||
System.Management.Automation.ErrorRecord | ||
.NOTES | ||
The function supports two parameter sets: 'ErrorRecord' and 'Exception'. | ||
If the 'ErrorRecord' parameter set is used, the function creates a new ErrorRecord based on an existing one and an exception. | ||
If the 'Exception' parameter set is used, the function creates a new ErrorRecord based on an exception, an error category, a target object, and an error ID. | ||
#> | ||
function New-ErrorRecord | ||
{ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'The command does not change state.')] | ||
[CmdletBinding(DefaultParameterSetName = 'Exception')] | ||
[OutputType([System.Management.Automation.ErrorRecord])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true, ParameterSetName = 'ErrorRecord')] | ||
[System.Management.Automation.ErrorRecord] | ||
$ErrorRecord, | ||
|
||
[Parameter(Mandatory = $true, ParameterSetName = 'ErrorRecord')] | ||
[Parameter(Mandatory = $true, ParameterSetName = 'Exception')] | ||
[System.Exception] | ||
$Exception, | ||
|
||
[Parameter(Mandatory = $true, ParameterSetName = 'Exception')] | ||
[System.Management.Automation.ErrorCategory] | ||
$ErrorCategory, | ||
|
||
[Parameter(ParameterSetName = 'Exception')] | ||
[System.Object] | ||
$TargetObject = $null, | ||
|
||
[Parameter(ParameterSetName = 'Exception')] | ||
[System.String] | ||
$ErrorId = $null | ||
) | ||
|
||
switch ($PSCmdlet.ParameterSetName) | ||
{ | ||
'ErrorRecord' | ||
{ | ||
$errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList @( | ||
$ErrorRecord, | ||
$Exception | ||
) | ||
|
||
break | ||
} | ||
|
||
'Exception' | ||
{ | ||
$errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList @( | ||
$Exception, | ||
$ErrorId, | ||
$ErrorCategory, | ||
$TargetObject | ||
) | ||
|
||
break | ||
} | ||
} | ||
|
||
return $errorRecord | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] | ||
param () | ||
|
||
BeforeDiscovery { | ||
try | ||
{ | ||
if (-not (Get-Module -Name 'DscResource.Test')) | ||
{ | ||
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. | ||
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) | ||
{ | ||
# Redirect all streams to $null, except the error stream (stream 2) | ||
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null | ||
} | ||
|
||
# If the dependencies has not been resolved, this will throw an error. | ||
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' | ||
} | ||
} | ||
catch [System.IO.FileNotFoundException] | ||
{ | ||
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' | ||
} | ||
} | ||
|
||
BeforeAll { | ||
$script:moduleName = 'DscResource.Common' | ||
|
||
# Make sure there are not other modules imported that will conflict with mocks. | ||
Get-Module -Name $script:moduleName -All | Remove-Module -Force | ||
|
||
# Re-import the module using force to get any code changes between runs. | ||
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' | ||
|
||
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName | ||
$PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName | ||
$PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName | ||
} | ||
|
||
AfterAll { | ||
$PSDefaultParameterValues.Remove('Mock:ModuleName') | ||
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName') | ||
$PSDefaultParameterValues.Remove('Should:ModuleName') | ||
|
||
Remove-Module -Name $script:moduleName | ||
} | ||
|
||
Describe 'New-ErrorRecord' { | ||
Context 'ErrorRecord parameter set' { | ||
It 'creates a new ErrorRecord based on an existing one and an exception' { | ||
$existingErrorRecord = [System.Management.Automation.ErrorRecord]::new( | ||
[System.Management.Automation.ParentContainsErrorRecordException]::new('Existing error'), | ||
'ExistingErrorId', | ||
[System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
$null | ||
) | ||
$newException = [System.Exception]::new('New error') | ||
$newErrorRecord = New-ErrorRecord -ErrorRecord $existingErrorRecord -Exception $newException | ||
|
||
$newErrorRecord.Exception.Message | Should -Be 'New error' | ||
$newErrorRecord.FullyQualifiedErrorId | Should -Be 'ExistingErrorId' | ||
$newErrorRecord.CategoryInfo.Category | Should -Be 'InvalidOperation' | ||
} | ||
} | ||
|
||
Context 'Exception parameter set' { | ||
It 'creates a new ErrorRecord based on an exception, an error category, a target object, and an error ID' { | ||
$exception = [System.Exception]::new('An error occurred.') | ||
$targetObject = New-Object -TypeName PSObject | ||
$errorRecord = New-ErrorRecord -Exception $exception -ErrorCategory 'InvalidOperation' -TargetObject $targetObject -ErrorId 'MyErrorId' | ||
|
||
$errorRecord.Exception.Message | Should -Be 'An error occurred.' | ||
$errorRecord.FullyQualifiedErrorId | Should -Be 'MyErrorId' | ||
$errorRecord.CategoryInfo.Category | Should -Be 'InvalidOperation' | ||
$errorRecord.TargetObject | Should -Be $targetObject | ||
} | ||
} | ||
} |