Don't format exceptions that aren't ErrorRecords (#11415)

* In one of the previous PRs for ConciseView, System.Exception was incorrectly added to the typenames handled by the ErrorRecord formatting. This results in just Exception objects written to the console to not be rendered since the ErrorRecord formatting didn't handle this situation. Fix is to remove the line to add that type putting it back to how it was previously. 

* Also made the handling of ErrorRecord a bit more robust just in case Exception.Message doesn't exist nor a Message property.
This commit is contained in:
Steve Lee
2020-01-09 15:59:06 -08:00
committed by Dongbo Wang
parent 1cdbf5636a
commit fdcf087654
2 changed files with 12 additions and 2 deletions
@@ -146,7 +146,6 @@ namespace System.Management.Automation.Runspaces
var errorRecord_Exception = new ExtendedTypeDefinition(
"System.Management.Automation.ErrorRecord",
ViewsOf_System_Management_Automation_ErrorRecord());
errorRecord_Exception.TypeNames.Add("System.Exception");
yield return errorRecord_Exception;
yield return new ExtendedTypeDefinition(
@@ -1129,9 +1128,15 @@ namespace System.Management.Automation.Runspaces
# need to parse out the relevant part of the pre-rendered positionmessage
$message += $err.Exception.Message.split(""~`n"")[1].split(""${newline}${newline}"")[0]
}
else {
elseif ($err.Exception) {
$message += $err.Exception.Message
}
elseif ($err.Message) {
$message += $err.Message
}
else {
$message += $err.ToString()
}
}
else {
$message += $err.ErrorDetails.Message
@@ -13,6 +13,11 @@ Describe 'Tests for $ErrorView' -Tag CI {
$ErrorView | Should -BeExactly $expectedDefault
}
It 'Exceptions not thrown do not get formatted as ErrorRecord' {
$exp = [System.Exception]::new('test') | Out-String
$exp | Should -BeLike "*Message : test*"
}
Context 'ConciseView tests' {
It 'Cmdlet error should be one line of text' {