Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -946,14 +946,29 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
$isFirstElement = $true
foreach ($value in $prop.Value) {
$null = $output.Append($newline)
if (!$isFirstElement) {
$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)))
}
$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 {
Expand Down
107 changes: 107 additions & 0 deletions test/powershell/Modules/Microsoft.PowerShell.Core/Get-Error.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "Get-Error" -Tags "CI" {
It "Does not hang when serializing exception with array with type instances" {
$ps = [PowerShell]::Create()
$null = $ps.AddScript(@'
class GetErrorWithTypeArray : Exception {
[type[]]$Values
[type]$Type

GetErrorWithTypeArray ([string]$Message) : base($Message) {
$this.Values = [type[]]@([string], [int])
$this.Type = [bool]
}
}
try { throw [GetErrorWithTypeArray]::new("") } catch {}
Get-Error | Out-String
'@)

$task = $ps.BeginInvoke()
if (-not $task.AsyncWaitHandle.WaitOne(5000)) {
$null = $ps.BeginStop($null, $null)
throw "Timed out waiting for Get-Error to serialize"
}

$result = $ps.EndInvoke($task)
$result.Count | Should -Be 1
$result[0] | Should -BeOfType ([string])

$formattedError = (@(
$result[0] -split "\r?\n" | ForEach-Object {
$_.TrimEnd()
}
) -join ([Environment]::NewLine)).Trim()

$formattedError | Should -Be @'
Exception :
Values :
[System.String]
[System.Int32]
Type : [System.Boolean]
HResult : -2146233088
CategoryInfo : OperationStopped: (:) [], GetErrorWithTypeArray
InvocationInfo :
ScriptLineNumber : 10
OffsetInLine : 19
HistoryId : 1
Line : try { throw [GetErrorWithTypeArray]::new("") } catch {}

Statement : throw [GetErrorWithTypeArray]::new("")
PositionMessage : At line:10 char:19
+ try { throw [GetErrorWithTypeArray]::new("") } catch {}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 10
'@.Trim()
}

It "Formats strings and primitive types in an array" {
$ps = [PowerShell]::Create()
$null = $ps.AddScript(@'
class GetErrorPrimitiveArray : Exception {
[object[]]$Values

GetErrorPrimitiveArray ([string]$Message) : base($Message) {
$this.Values = @(1, "alpha", 0.5)
}
}
try { throw [GetErrorPrimitiveArray]::new("") } catch {}
Get-Error | Out-String
'@)

$result = $ps.Invoke()
$result.Count | Should -Be 1
$result[0] | Should -BeOfType ([string])

$formattedError = (@(
$result[0] -split "\r?\n" | ForEach-Object {
$_.TrimEnd()
}
) -join ([Environment]::NewLine)).Trim()

$formattedError | Should -Be @'
Exception :
Type : GetErrorPrimitiveArray
Values :
1
alpha
0.5
HResult : -2146233088
CategoryInfo : OperationStopped: (:) [], GetErrorPrimitiveArray
InvocationInfo :
ScriptLineNumber : 8
OffsetInLine : 19
HistoryId : 1
Line : try { throw [GetErrorPrimitiveArray]::new("") } catch {}

Statement : throw [GetErrorPrimitiveArray]::new("")
PositionMessage : At line:8 char:19
+ try { throw [GetErrorPrimitiveArray]::new("") } catch {}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 8
'@.Trim()
}
}