Skip to content

Commit ec5a776

Browse files
authored
When formatting, if collection is modified, don't fail the entire pipeline (#14438)
1 parent 7e9118c commit ec5a776

File tree

1 file changed

+11
-3
lines changed
  • src/System.Management.Automation/FormatAndOutput/common/Utilities

1 file changed

+11
-3
lines changed

src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.ObjectModel;
88
using System.Globalization;
99
using System.Management.Automation;
10+
using System.Management.Automation.Internal;
1011
using System.Management.Automation.Runspaces;
1112
using System.Reflection;
1213
using System.Text;
@@ -19,6 +20,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
1920
/// </summary>
2021
internal static class PSObjectHelper
2122
{
23+
#region tracer
24+
[TraceSource("PSObjectHelper", "PSObjectHelper")]
25+
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper");
26+
#endregion tracer
27+
2228
internal const char Ellipsis = '\u2026';
2329

2430
internal static string PSObjectIsOfExactType(Collection<string> typeNames)
@@ -290,10 +296,12 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex
290296
// take care of the case there is no base object
291297
return so.ToString();
292298
}
293-
catch (ExtendedTypeSystemException e)
299+
catch (Exception e) when (e is ExtendedTypeSystemException || e is InvalidOperationException)
294300
{
295-
// NOTE: we catch all the exceptions, since we do not know
296-
// what the underlying object access would throw
301+
// These exceptions are being caught and handled by returning an empty string when
302+
// the object cannot be stringified due to ETS or an instance in the collection has been modified
303+
s_tracer.TraceWarning($"SmartToString method: Exception during conversion to string, emitting empty string: {e.Message}");
304+
297305
if (formatErrorObject != null)
298306
{
299307
formatErrorObject.sourceObject = so;

0 commit comments

Comments
 (0)