Add Type member to Exceptions containing type of exception for Get-Error (#11076)

This commit is contained in:
Steve Lee
2020-01-09 15:34:24 -08:00
committed by Dongbo Wang
parent 1d48608f62
commit c5bc120cff
2 changed files with 25 additions and 0 deletions
@@ -783,6 +783,11 @@ namespace System.Management.Automation.Runspaces
'System.Management.Automation.InvocationInfo'
)
# if object is an Exception, add an ExceptionType property
if ($obj -is [Exception]) {
$obj | Add-Member -NotePropertyName Type -NotePropertyValue $obj.GetType().FullName -ErrorAction Ignore
}
# first find the longest property so we can indent properly
$propLength = 0
foreach ($prop in $obj.PSObject.Properties) {
@@ -105,4 +105,24 @@ Describe 'Get-Error tests' -Tag CI {
$Error[0].pstypenames | Should -Be System.Management.Automation.ErrorRecord, System.Object
}
It 'Get-Error adds ExceptionType for Exceptions' {
try {
[System.Net.DNS]::GetHostByName((New-Guid))
}
catch {
}
$out = Get-Error | Out-String
$out | Should -BeLikeExactly '*Type*'
if ($IsWindows) {
$expectedExceptionType = "System.Management.Automation.ParentContainsErrorRecordException"
}
else {
$expectedExceptionType = "System.Net.Internals.SocketExceptionFactory+ExtendedSocketException"
}
$out | Should -BeLikeExactly "*$expectedExceptionType*"
}
}