mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add Type member to Exceptions containing type of exception for Get-Error (#11076)
This commit is contained in:
+5
@@ -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*"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user