When formatting, if collection is modified, don't fail the entire pipeline (#14438)

This commit is contained in:
Steve Lee authored and GitHub committed 2021-01-27 16:37:36 -08:00
1 parent 7e9118c7e0
commit ec5a77653c
1 file changed
+11 -3
@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Text;
@@ -19,6 +20,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// </summary>
internal static class PSObjectHelper
{
#region tracer
[TraceSource("PSObjectHelper", "PSObjectHelper")]
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper");
#endregion tracer
internal const char Ellipsis = '\u2026';
internal static string PSObjectIsOfExactType(Collection<string> typeNames)
@@ -290,10 +296,12 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
// take care of the case there is no base object
return so.ToString();
}
catch (ExtendedTypeSystemException e)
catch (Exception e) when (e is ExtendedTypeSystemException || e is InvalidOperationException)
{
// NOTE: we catch all the exceptions, since we do not know
// what the underlying object access would throw
// These exceptions are being caught and handled by returning an empty string when
// the object cannot be stringified due to ETS or an instance in the collection has been modified
s_tracer.TraceWarning($"SmartToString method: Exception during conversion to string, emitting empty string: {e.Message}");
if (formatErrorObject != null)
{
formatErrorObject.sourceObject = so;