From d4cabbe5a393670870b06ccd8959fa5facd89123 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 18 Jun 2021 15:02:22 -0700 Subject: [PATCH] Add `DetailedView` to `$ErrorView` --- .../PowerShellCore_format_ps1xml.cs | 7 +++++-- .../engine/CommandBase.cs | 3 +++ .../engine/Formatting/ErrorView.Tests.ps1 | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index b29fe0bb5de..d466663631f 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -996,7 +996,7 @@ private static IEnumerable ViewsOf_System_Management_Autom CustomControl.Create(outOfBand: true) .StartEntry() .AddScriptBlockExpressionBinding(@" - if (@('NativeCommandErrorMessage','NativeCommandError') -notcontains $_.FullyQualifiedErrorId -and @('CategoryView','ConciseView') -notcontains $ErrorView) + if (@('NativeCommandErrorMessage','NativeCommandError') -notcontains $_.FullyQualifiedErrorId -and @('CategoryView','ConciseView','DetailedView') -notcontains $ErrorView) { $myinv = $_.InvocationInfo if ($myinv -and $myinv.MyCommand) @@ -1286,7 +1286,10 @@ function Get-ConciseViewPositionMessage { else { $myinv = $err.InvocationInfo - if ($ErrorView -eq 'ConciseView') { + if ($ErrorView -eq 'DetailedView') { + return (Get-Error | Out-String) + } + elseif ($ErrorView -eq 'ConciseView') { $posmsg = Get-ConciseViewPositionMessage } elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) { diff --git a/src/System.Management.Automation/engine/CommandBase.cs b/src/System.Management.Automation/engine/CommandBase.cs index 2e1c5cda56c..016b22ff596 100644 --- a/src/System.Management.Automation/engine/CommandBase.cs +++ b/src/System.Management.Automation/engine/CommandBase.cs @@ -300,6 +300,9 @@ public enum ErrorView /// Concise shows more information on the context of the error or just the message if not a script or parser error. ConciseView = 2, + + /// Detailed will leverage Get-Error to get much more detailed information for the error. + DetailedView = 3, } #endregion ErrorView diff --git a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 index b94c4b5fa4c..21c886d5ae8 100644 --- a/test/powershell/engine/Formatting/ErrorView.Tests.ps1 +++ b/test/powershell/engine/Formatting/ErrorView.Tests.ps1 @@ -170,4 +170,20 @@ Describe 'Tests for $ErrorView' -Tag CI { $e | Should -BeLike '*Oops!*' } } + + Context 'DetailedView tests' { + + It 'Detailed error is rendered' { + try { + $ErrorView = 'DetailedView' + throw 'Oops!' + } + catch { + # an extra newline gets added by the formatting system so we remove them + $e = ($_ | Out-String).Trim([Environment]::NewLine.ToCharArray()) + } + + $e | Should -BeExactly (Get-Error | Out-String).Trim([Environment]::NewLine.ToCharArray()) + } + } }