diff --git a/Reference/FileInfo.format.ps1xml b/Reference/FileInfo.format.ps1xml new file mode 100644 index 0000000..745a045 --- /dev/null +++ b/Reference/FileInfo.format.ps1xml @@ -0,0 +1,206 @@ +<?xml version="1.0" encoding="utf-8"?> +<Configuration> + <ViewDefinitions> + <View> + <Name>children</Name> + <ViewSelectedBy> + <TypeName>System.IO.DirectoryInfo</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSParentPath</PropertyName> + </GroupBy> + <TableControl> + <TableHeaders> + <TableColumnHeader> + <Label>Mode</Label> + <Width>7</Width> + <Alignment>Left</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>LastWriteTime</Label> + <Width>26</Width> + <Alignment>Right</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>Length</Label> + <Width>14</Width> + <Alignment>Right</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>Name</Label> + <Alignment>Left</Alignment> + </TableColumnHeader> + </TableHeaders> + <TableRowEntries> + <TableRowEntry> + <Wrap /> + <TableColumnItems> + <TableColumnItem> + <PropertyName>ModeWithoutHardLink</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>LastWriteTimeString</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>LengthString</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>NameString</PropertyName> + </TableColumnItem> + </TableColumnItems> + </TableRowEntry> + </TableRowEntries> + </TableControl> + </View> + <View> + <Name>childrenWithHardlink</Name> + <ViewSelectedBy> + <TypeName>System.IO.DirectoryInfo</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSParentPath</PropertyName> + </GroupBy> + <TableControl> + <TableHeaders> + <TableColumnHeader> + <Label>Mode</Label> + <Width>7</Width> + <Alignment>Left</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>LastWriteTime</Label> + <Width>26</Width> + <Alignment>Right</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>Length</Label> + <Width>14</Width> + <Alignment>Right</Alignment> + </TableColumnHeader> + <TableColumnHeader> + <Label>Name</Label> + <Alignment>Left</Alignment> + </TableColumnHeader> + </TableHeaders> + <TableRowEntries> + <TableRowEntry> + <Wrap /> + <TableColumnItems> + <TableColumnItem> + <PropertyName>Mode</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>LastWriteTimeString</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>LengthString</PropertyName> + </TableColumnItem> + <TableColumnItem> + <PropertyName>NameString</PropertyName> + </TableColumnItem> + </TableColumnItems> + </TableRowEntry> + </TableRowEntries> + </TableControl> + </View> + <View> + <Name>children</Name> + <ViewSelectedBy> + <TypeName>System.IO.DirectoryInfo</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSParentPath</PropertyName> + </GroupBy> + <ListControl> + <ListEntries> + <ListEntry> + <ListItems> + <ListItem> + <PropertyName>Name</PropertyName> + </ListItem> + <ListItem> + <PropertyName>CreationTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LastWriteTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LastAccessTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>Mode</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LinkType</PropertyName> + </ListItem> + <ListItem> + <PropertyName>Target</PropertyName> + </ListItem> + </ListItems> + </ListEntry> + <ListEntry> + <EntrySelectedBy> + <TypeName>System.IO.FileInfo</TypeName> + </EntrySelectedBy> + <ListItems> + <ListItem> + <PropertyName>Name</PropertyName> + </ListItem> + <ListItem> + <Label>Length</Label> + <PropertyName>LengthString</PropertyName> + </ListItem> + <ListItem> + <PropertyName>CreationTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LastWriteTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LastAccessTime</PropertyName> + </ListItem> + <ListItem> + <PropertyName>Mode</PropertyName> + </ListItem> + <ListItem> + <PropertyName>LinkType</PropertyName> + </ListItem> + <ListItem> + <PropertyName>Target</PropertyName> + </ListItem> + <ListItem> + <PropertyName>VersionInfo</PropertyName> + </ListItem> + </ListItems> + </ListEntry> + </ListEntries> + </ListControl> + </View> + <View> + <Name>children</Name> + <ViewSelectedBy> + <TypeName>System.IO.DirectoryInfo</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSParentPath</PropertyName> + </GroupBy> + <WideControl> + <WideEntries> + <WideEntry> + <WideItem> + <PropertyName>Name</PropertyName> + </WideItem> + </WideEntry> + <WideEntry> + <EntrySelectedBy> + <TypeName>System.IO.DirectoryInfo</TypeName> + </EntrySelectedBy> + <WideItem> + <PropertyName>Name</PropertyName> + </WideItem> + </WideEntry> + </WideEntries> + </WideControl> + </View> + </ViewDefinitions> +</Configuration> \ No newline at end of file diff --git a/Reference/LegacyErrorView.format.ps1xml b/Reference/LegacyErrorView.format.ps1xml new file mode 100644 index 0000000..fde1bd3 --- /dev/null +++ b/Reference/LegacyErrorView.format.ps1xml @@ -0,0 +1 @@ +<?xml version="1.0" encoding="utf-8"?><Configuration><ViewDefinitions /></Configuration> \ No newline at end of file diff --git a/Reference/OriginalErrorView.format.ps1xml b/Reference/OriginalErrorView.format.ps1xml new file mode 100644 index 0000000..835a083 --- /dev/null +++ b/Reference/OriginalErrorView.format.ps1xml @@ -0,0 +1,600 @@ +<?xml version="1.0" encoding="utf-8"?> +<Configuration> + <ViewDefinitions> + <View> + <Name>GetErrorInstance</Name> + <ViewSelectedBy> + <TypeName>System.Management.Automation.ErrorRecord#PSExtendedError</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSErrorIndex</PropertyName> + <Label>ErrorIndex</Label> + </GroupBy> + <CustomControl> + <CustomEntries> + <CustomEntry> + <CustomItem> + <ExpressionBinding> + <ScriptBlock> + Set-StrictMode -Off + + $maxDepth = 10 + $ellipsis = "`u{2026}" + $resetColor = '' + $errorColor = '' + $accentColor = '' + + if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) { + $resetColor = $PSStyle.Reset + $errorColor = $psstyle.Formatting.Error + $accentColor = $PSStyle.Formatting.FormatAccent + } + + function Show-ErrorRecord($obj, [int]$indent = 0, [int]$depth = 1) { + $newline = [Environment]::Newline + $output = [System.Text.StringBuilder]::new() + $prefix = ' ' * $indent + + $expandTypes = @( + 'Microsoft.Rest.HttpRequestMessageWrapper' + 'Microsoft.Rest.HttpResponseMessageWrapper' + '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) { + if ($prop.Value -ne $null -and $prop.Value -ne [string]::Empty -and $prop.Name.Length -gt $propLength) { + $propLength = $prop.Name.Length + } + } + + $addedProperty = $false + foreach ($prop in $obj.PSObject.Properties) { + + # don't show empty properties or our added property for $error[index] + if ($prop.Value -ne $null -and $prop.Value -ne [string]::Empty -and $prop.Value.count -gt 0 -and $prop.Name -ne 'PSErrorIndex') { + $addedProperty = $true + $null = $output.Append($prefix) + $null = $output.Append($accentColor) + $null = $output.Append($prop.Name) + $propNameIndent = ' ' * ($propLength - $prop.Name.Length) + $null = $output.Append($propNameIndent) + $null = $output.Append(' : ') + $null = $output.Append($resetColor) + + $newIndent = $indent + 4 + + # only show nested objects that are Exceptions, ErrorRecords, or types defined in $expandTypes and types not in $ignoreTypes + if ($prop.Value -is [Exception] -or $prop.Value -is [System.Management.Automation.ErrorRecord] -or + $expandTypes -contains $prop.TypeNameOfValue -or ($prop.TypeNames -ne $null -and $expandTypes -contains $prop.TypeNames[0])) { + + if ($depth -ge $maxDepth) { + $null = $output.Append($ellipsis) + } + else { + $null = $output.Append($newline) + $null = $output.Append((Show-ErrorRecord $prop.Value $newIndent ($depth + 1))) + } + } + # `TargetSite` has many members that are not useful visually, so we have a reduced view of the relevant members + elseif ($prop.Name -eq 'TargetSite' -and $prop.Value.GetType().Name -eq 'RuntimeMethodInfo') { + if ($depth -ge $maxDepth) { + $null = $output.Append($ellipsis) + } + else { + $targetSite = [PSCustomObject]@{ + Name = $prop.Value.Name + DeclaringType = $prop.Value.DeclaringType + MemberType = $prop.Value.MemberType + Module = $prop.Value.Module + } + + $null = $output.Append($newline) + $null = $output.Append((Show-ErrorRecord $targetSite $newIndent ($depth + 1))) + } + } + # `StackTrace` is handled specifically because the lines are typically long but necessary so they are left justified without additional indentation + elseif ($prop.Name -eq 'StackTrace') { + # for a stacktrace which is usually quite wide with info, we left justify it + $null = $output.Append($newline) + $null = $output.Append($prop.Value) + } + # Dictionary and Hashtable we want to show as Key/Value pairs, we don't do the extra whitespace alignment here + elseif ($prop.Value.GetType().Name.StartsWith('Dictionary') -or $prop.Value.GetType().Name -eq 'Hashtable') { + $isFirstElement = $true + foreach ($key in $prop.Value.Keys) { + if ($isFirstElement) { + $null = $output.Append($newline) + } + + if ($key -eq 'Authorization') { + $null = $output.Append("${prefix} ${accentColor}${key} : ${resetColor}${ellipsis}${newline}") + } + else { + $null = $output.Append("${prefix} ${accentColor}${key} : ${resetColor}$($prop.Value[$key])${newline}") + } + + $isFirstElement = $false + } + } + # if the object implements IEnumerable and not a string, we try to show each object + # We ignore the `Data` property as it can contain lots of type information by the interpreter that isn't useful here + elseif (!($prop.Value -is [System.String]) -and $prop.Value.GetType().GetInterface('IEnumerable') -ne $null -and $prop.Name -ne 'Data') { + + if ($depth -ge $maxDepth) { + $null = $output.Append($ellipsis) + } + else { + $isFirstElement = $true + foreach ($value in $prop.Value) { + $null = $output.Append($newline) + $valueIndent = ' ' * ($newIndent + 2) + + if ($value -is [Type]) { + # Just show the typename instead of it as an object + $null = $output.Append("${prefix}${valueIndent}[$($value.ToString())]") + } + elseif ($value -is [string] -or $value.GetType().IsPrimitive) { + $null = $output.Append("${prefix}${valueIndent}${value}") + } + else { + if (!$isFirstElement) { + $null = $output.Append($newline) + } + $null = $output.Append((Show-ErrorRecord $value $newIndent ($depth + 1))) + } + $isFirstElement = $false + } + } + } + elseif ($prop.Value -is [Type]) { + # Just show the typename instead of it as an object + $null = $output.Append("[$($prop.Value.ToString())]") + } + # Anything else, we convert to string. + # ToString() can throw so we use LanguagePrimitives.TryConvertTo() to hide a convert error + else { + $value = $null + if ([System.Management.Automation.LanguagePrimitives]::TryConvertTo($prop.Value, [string], [ref]$value) -and $value -ne $null) + { + if ($prop.Name -eq 'PositionMessage') { + $value = $value.Insert($value.IndexOf('~'), $errorColor) + } + elseif ($prop.Name -eq 'Message') { + $value = $errorColor + $value + } + + $isFirstLine = $true + if ($value.Contains($newline)) { + # the 3 is to account for ' : ' + $valueIndent = ' ' * ($propLength + 3) + # need to trim any extra whitespace already in the text + foreach ($line in $value.Split($newline)) { + if (!$isFirstLine) { + $null = $output.Append("${newline}${prefix}${valueIndent}") + } + $null = $output.Append($line.Trim()) + $isFirstLine = $false + } + } + else { + $null = $output.Append($value) + } + } + } + + $null = $output.Append($newline) + } + } + + # if we had added nested properties, we need to remove the last newline + if ($addedProperty) { + $null = $output.Remove($output.Length - $newline.Length, $newline.Length) + } + + $output.ToString() + } + + # Add back original typename and remove PSExtendedError + if ($_.PSObject.TypeNames.Contains('System.Management.Automation.ErrorRecord#PSExtendedError')) { + $_.PSObject.TypeNames.Add('System.Management.Automation.ErrorRecord') + $null = $_.PSObject.TypeNames.Remove('System.Management.Automation.ErrorRecord#PSExtendedError') + } + elseif ($_.PSObject.TypeNames.Contains('System.Exception#PSExtendedError')) { + $_.PSObject.TypeNames.Add('System.Exception') + $null = $_.PSObject.TypeNames.Remove('System.Exception#PSExtendedError') + } + + Show-ErrorRecord $_ + </ScriptBlock> + </ExpressionBinding> + </CustomItem> + </CustomEntry> + </CustomEntries> + </CustomControl> + </View> + <View> + <Name>ErrorInstance</Name> + <ViewSelectedBy> + <TypeName>System.Management.Automation.ErrorRecord</TypeName> + </ViewSelectedBy> + <OutOfBand /> + <CustomControl> + <CustomEntries> + <CustomEntry> + <CustomItem> + <ExpressionBinding> + <ScriptBlock> $errorColor = '' + $commandPrefix = '' + if (@('NativeCommandErrorMessage','NativeCommandError') -notcontains $_.FullyQualifiedErrorId -and @('CategoryView','ConciseView','DetailedView') -notcontains $ErrorView) + { + $myinv = $_.InvocationInfo + if ($Host.UI.SupportsVirtualTerminal) { + $errorColor = $PSStyle.Formatting.Error + } + + $commandPrefix = if ($myinv -and $myinv.MyCommand) { + switch -regex ( $myinv.MyCommand.CommandType ) + { + ([System.Management.Automation.CommandTypes]::ExternalScript) + { + if ($myinv.MyCommand.Path) + { + $myinv.MyCommand.Path + ' : ' + } + + break + } + + ([System.Management.Automation.CommandTypes]::Script) + { + if ($myinv.MyCommand.ScriptBlock) + { + $myinv.MyCommand.ScriptBlock.ToString() + ' : ' + } + + break + } + default + { + if ($myinv.InvocationName -match '^[&\.]?$') + { + if ($myinv.MyCommand.Name) + { + $myinv.MyCommand.Name + ' : ' + } + } + else + { + $myinv.InvocationName + ' : ' + } + + break + } + } + } + elseif ($myinv -and $myinv.InvocationName) + { + $myinv.InvocationName + ' : ' + } + } + + $errorColor + $commandPrefix</ScriptBlock> + </ExpressionBinding> + <ExpressionBinding> + <ScriptBlock> Set-StrictMode -Off + $ErrorActionPreference = 'Stop' + trap { 'Error found in error view definition: ' + $_.Exception.Message } + $newline = [Environment]::Newline + + $resetColor = '' + $errorColor = '' + $accentColor = '' + + if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) { + $resetColor = $PSStyle.Reset + $errorColor = $PSStyle.Formatting.Error + $accentColor = $PSStyle.Formatting.ErrorAccent + } + + function Get-ConciseViewPositionMessage { + + # returns a string cut to last whitespace + function Get-TruncatedString($string, [int]$length) { + + if ($string.Length -le $length) { + return $string + } + + return ($string.Substring(0,$length) -split '\s',-2)[0] + } + + $posmsg = '' + $headerWhitespace = '' + $offsetWhitespace = '' + $message = '' + $prefix = '' + + # Handle case where there is a TargetObject from a Pester `Should` assertion failure and we can show the error at the target rather than the script source + # Note that in some versions, this is a Dictionary<,> and in others it's a hashtable. So we explicitly cast to a shared interface in the method invocation + # to force using `IDictionary.Contains`. Hashtable does have it's own `ContainKeys` as well, but if they ever opt to use a custom `IDictionary`, that may not. + $useTargetObject = $null -ne $err.TargetObject -and + $err.TargetObject -is [System.Collections.IDictionary] -and + ([System.Collections.IDictionary]$err.TargetObject).Contains('Line') -and + ([System.Collections.IDictionary]$err.TargetObject).Contains('LineText') + + # The checks here determine if we show line detailed error information: + # - check if `ParserError` and comes from PowerShell which eventually results in a ParseException, but during this execution it's an ErrorRecord + $isParseError = $err.CategoryInfo.Category -eq 'ParserError' -and + $err.Exception -is [System.Management.Automation.ParentContainsErrorRecordException] + + # - check if invocation is a script or multiple lines in the console + $isMultiLineOrExternal = $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1 + + # - check that it's not a script module as expectation is that users don't want to see the line of error within a module + $shouldShowLineDetail = ($isParseError -or $isMultiLineOrExternal) -and + $myinv.ScriptName -notmatch '\.psm1$' + + if ($useTargetObject -or $shouldShowLineDetail) { + + if ($useTargetObject) { + $posmsg = "${resetcolor}$($err.TargetObject.File)${newline}" + } + elseif ($myinv.ScriptName) { + if ($env:TERM_PROGRAM -eq 'vscode') { + # If we are running in vscode, we know the file:line:col links are clickable so we use this format + $posmsg = "${resetcolor}$($myinv.ScriptName):$($myinv.ScriptLineNumber):$($myinv.OffsetInLine)${newline}" + } + else { + $posmsg = "${resetcolor}$($myinv.ScriptName):$($myinv.ScriptLineNumber)${newline}" + } + } + else { + $posmsg = "${newline}" + } + + if ($useTargetObject) { + $scriptLineNumber = $err.TargetObject.Line + $scriptLineNumberLength = $err.TargetObject.Line.ToString().Length + } + else { + $scriptLineNumber = $myinv.ScriptLineNumber + $scriptLineNumberLength = $myinv.ScriptLineNumber.ToString().Length + } + + if ($scriptLineNumberLength -gt 4) { + $headerWhitespace = ' ' * ($scriptLineNumberLength - 4) + } + + $lineWhitespace = '' + if ($scriptLineNumberLength -lt 4) { + $lineWhitespace = ' ' * (4 - $scriptLineNumberLength) + } + + $verticalBar = '|' + $posmsg += "${accentColor}${headerWhitespace}Line ${verticalBar}${newline}" + + $highlightLine = '' + if ($useTargetObject) { + $line = $_.TargetObject.LineText.Trim() + $offsetLength = 0 + $offsetInLine = 0 + } + else { + $positionMessage = $myinv.PositionMessage.Split($newline) + $line = $positionMessage[1].Substring(1) # skip the '+' at the start + $highlightLine = $positionMessage[$positionMessage.Count - 1].Substring(1) + $offsetLength = $highlightLine.Trim().Length + $offsetInLine = $highlightLine.IndexOf('~') + } + + if (-not $line.EndsWith($newline)) { + $line += $newline + } + + # don't color the whole line + if ($offsetLength -lt $line.Length - 1) { + $line = $line.Insert($offsetInLine + $offsetLength, $resetColor).Insert($offsetInLine, $accentColor) + } + + $posmsg += "${accentColor}${lineWhitespace}${ScriptLineNumber} ${verticalBar} ${resetcolor}${line}" + $offsetWhitespace = ' ' * $offsetInLine + $prefix = "${accentColor}${headerWhitespace} ${verticalBar} ${errorColor}" + if ($highlightLine -ne '') { + $posMsg += "${prefix}${highlightLine}${newline}" + } + $message = "${prefix}" + } + + if (! $err.ErrorDetails -or ! $err.ErrorDetails.Message) { + if ($err.CategoryInfo.Category -eq 'ParserError' -and $err.Exception.Message.Contains("~$newline")) { + # need to parse out the relevant part of the pre-rendered positionmessage + $message += $err.Exception.Message.split("~$newline")[1].split("${newline}${newline}")[0] + } + elseif ($err.Exception) { + $message += $err.Exception.Message + } + elseif ($err.Message) { + $message += $err.Message + } + else { + $message += $err.ToString() + } + } + else { + $message += $err.ErrorDetails.Message + } + + # if rendering line information, break up the message if it's wider than the console + if ($myinv -and $myinv.ScriptName -or $err.CategoryInfo.Category -eq 'ParserError') { + $prefixLength = [System.Management.Automation.Internal.StringDecorated]::new($prefix).ContentLength + $prefixVtLength = $prefix.Length - $prefixLength + + # replace newlines in message so it lines up correct + $message = $message.Replace($newline, ' ').Replace("`n", ' ').Replace("`t", ' ') + + $windowWidth = 120 + if ($Host.UI.RawUI -ne $null) { + $windowWidth = $Host.UI.RawUI.WindowSize.Width + } + + if ($windowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt $windowWidth) { + $sb = [Text.StringBuilder]::new() + $substring = Get-TruncatedString -string $message -length ($windowWidth + $prefixVTLength) + $null = $sb.Append($substring) + $remainingMessage = $message.Substring($substring.Length).Trim() + $null = $sb.Append($newline) + while (($remainingMessage.Length + $prefixLength) -gt $windowWidth) { + $subMessage = $prefix + $remainingMessage + $substring = Get-TruncatedString -string $subMessage -length ($windowWidth + $prefixVtLength) + + if ($substring.Length - $prefix.Length -gt 0) + { + $null = $sb.Append($substring) + $null = $sb.Append($newline) + $remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim() + } + else + { + break + } + } + $null = $sb.Append($prefix + $remainingMessage.Trim()) + $message = $sb.ToString() + } + + $message += $newline + } + + $posmsg += "${errorColor}" + $message + + $reason = 'Error' + if ($err.Exception -and $err.Exception.WasThrownFromThrowStatement) { + $reason = 'Exception' + } + # MyCommand can be the script block, so we don't want to show that so check if it's an actual command + elseif ($myinv.MyCommand -and $myinv.MyCommand.Name -and (Get-Command -Name $myinv.MyCommand -ErrorAction Ignore)) + { + $reason = $myinv.MyCommand + } + # If it's a scriptblock, better to show the command in the scriptblock that had the error + elseif ($err.CategoryInfo.Activity) { + $reason = $err.CategoryInfo.Activity + } + elseif ($myinv.MyCommand) { + $reason = $myinv.MyCommand + } + elseif ($myinv.InvocationName) { + $reason = $myinv.InvocationName + } + elseif ($err.CategoryInfo.Category) { + $reason = $err.CategoryInfo.Category + } + elseif ($err.CategoryInfo.Reason) { + $reason = $err.CategoryInfo.Reason + } + + $errorMsg = 'Error' + + "${errorColor}${reason}: ${posmsg}${resetcolor}" + } + + $myinv = $_.InvocationInfo + $err = $_ + if (!$myinv -and $_.ErrorRecord -and $_.ErrorRecord.InvocationInfo) { + $err = $_.ErrorRecord + $myinv = $err.InvocationInfo + } + + if ($err.FullyQualifiedErrorId -eq 'NativeCommandErrorMessage' -or $err.FullyQualifiedErrorId -eq 'NativeCommandError') { + return "${errorColor}$($err.Exception.Message)${resetcolor}" + } + + if ($ErrorView -eq 'DetailedView') { + $message = Get-Error | Out-String + return "${errorColor}${message}${resetcolor}" + } + + if ($ErrorView -eq 'CategoryView') { + $message = $err.CategoryInfo.GetMessage() + return "${errorColor}${message}${resetcolor}" + } + + $posmsg = '' + if ($ErrorView -eq 'ConciseView') { + $posmsg = Get-ConciseViewPositionMessage + } + elseif ($myinv -and ($myinv.MyCommand -or ($err.CategoryInfo.Category -ne 'ParserError'))) { + $posmsg = $myinv.PositionMessage + if ($posmsg -ne '') { + $posmsg = $newline + $posmsg + } + } + + if ($err.PSMessageDetails) { + $posmsg = ' : ' + $err.PSMessageDetails + $posmsg + } + + if ($ErrorView -eq 'ConciseView') { + $recommendedAction = $_.ErrorDetails.RecommendedAction + if (-not [String]::IsNullOrWhiteSpace($recommendedAction)) { + $recommendedAction = $newline + + ${errorColor} + + ' Recommendation: ' + + $recommendedAction + + ${resetcolor} + } + + if ($err.PSMessageDetails) { + $posmsg = "${errorColor}${posmsg}" + } + return $posmsg + $recommendedAction + } + + $indent = 4 + + $errorCategoryMsg = $err.ErrorCategory_Message + + if ($null -ne $errorCategoryMsg) + { + $indentString = '+ CategoryInfo : ' + $err.ErrorCategory_Message + } + else + { + $indentString = '+ CategoryInfo : ' + $err.CategoryInfo + } + + $posmsg += $newline + $indentString + + $indentString = "+ FullyQualifiedErrorId : " + $err.FullyQualifiedErrorId + $posmsg += $newline + $indentString + + $originInfo = $err.OriginInfo + + if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) + { + $indentString = "+ PSComputerName : " + $originInfo.PSComputerName + $posmsg += $newline + $indentString + } + + $finalMsg = if ($err.ErrorDetails.Message) { + $err.ErrorDetails.Message + $posmsg + } else { + $err.Exception.Message + $posmsg + } + + "${errorColor}${finalMsg}${resetcolor}"</ScriptBlock> + </ExpressionBinding> + </CustomItem> + </CustomEntry> + </CustomEntries> + </CustomControl> + </View> + </ViewDefinitions> +</Configuration> \ No newline at end of file diff --git a/Reference/OriginalExceptionView.format.ps1xml b/Reference/OriginalExceptionView.format.ps1xml new file mode 100644 index 0000000..d6e176d --- /dev/null +++ b/Reference/OriginalExceptionView.format.ps1xml @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="utf-8"?> +<Configuration> + <ViewDefinitions> + <View> + <Name>GetErrorInstance</Name> + <ViewSelectedBy> + <TypeName>System.Management.Automation.ErrorRecord#PSExtendedError</TypeName> + </ViewSelectedBy> + <GroupBy> + <PropertyName>PSErrorIndex</PropertyName> + <Label>ErrorIndex</Label> + </GroupBy> + <CustomControl> + <CustomEntries> + <CustomEntry> + <CustomItem> + <ExpressionBinding> + <ScriptBlock> + Set-StrictMode -Off + + $maxDepth = 10 + $ellipsis = "`u{2026}" + $resetColor = '' + $errorColor = '' + $accentColor = '' + + if ($Host.UI.SupportsVirtualTerminal -and ([string]::IsNullOrEmpty($env:__SuppressAnsiEscapeSequences))) { + $resetColor = $PSStyle.Reset + $errorColor = $psstyle.Formatting.Error + $accentColor = $PSStyle.Formatting.FormatAccent + } + + function Show-ErrorRecord($obj, [int]$indent = 0, [int]$depth = 1) { + $newline = [Environment]::Newline + $output = [System.Text.StringBuilder]::new() + $prefix = ' ' * $indent + + $expandTypes = @( + 'Microsoft.Rest.HttpRequestMessageWrapper' + 'Microsoft.Rest.HttpResponseMessageWrapper' + 'System.Management.Automation.InvocationInfo' + ) + + # if object is an Exception, add an ExceptionType property +$ \ No newline at end of file