From ab49afcfdada0795b417199952a4ad895b9349b7 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 5 Aug 2020 17:50:57 -0700 Subject: [PATCH 1/8] When formatting, if an exception occurs during enumeration, don't fail the whole operation --- .../common/Utilities/MshObjectUtil.cs | 18 +++++++++++------- .../resources/FormatAndOut_format_xxx.resx | 3 +++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 902f6f3f9d6..8c2d1244ad2 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -7,6 +7,7 @@ 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; @@ -290,17 +291,20 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex // take care of the case there is no base object return so.ToString(); } - catch (ExtendedTypeSystemException e) + catch (Exception e) { - // NOTE: we catch all the exceptions, since we do not know - // what the underlying object access would throw - if (formatErrorObject != null) + if (e is ExtendedTypeSystemException || e is InvalidOperationException) { - formatErrorObject.sourceObject = so; - formatErrorObject.exception = e; + if (formatErrorObject != null) + { + formatErrorObject.sourceObject = so; + formatErrorObject.exception = e; + } + + return string.Empty; } - return string.Empty; + throw; } } diff --git a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx index 66cfdc8a25f..afdf6f13199 100644 --- a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx +++ b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx @@ -179,4 +179,7 @@ Failed to interpret format string "{0}". + + Object skipped: {0}. + From b353ffabd3c4af3a7ff7734fbea334c307afc130 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 7 Jan 2021 11:11:34 -0800 Subject: [PATCH 2/8] address Rob's feedback --- .../common/Utilities/MshObjectUtil.cs | 15 +++++---------- .../resources/FormatAndOut_format_xxx.resx | 3 --- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 8c2d1244ad2..17b7952a718 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -291,20 +291,15 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex // take care of the case there is no base object return so.ToString(); } - catch (Exception e) + catch (Exception e) when (e is ExtendedTypeSystemException || e is InvalidOperationException) { - if (e is ExtendedTypeSystemException || e is InvalidOperationException) + if (formatErrorObject != null) { - if (formatErrorObject != null) - { - formatErrorObject.sourceObject = so; - formatErrorObject.exception = e; - } - - return string.Empty; + formatErrorObject.sourceObject = so; + formatErrorObject.exception = e; } - throw; + return string.Empty; } } diff --git a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx index afdf6f13199..66cfdc8a25f 100644 --- a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx +++ b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx @@ -179,7 +179,4 @@ Failed to interpret format string "{0}". - - Object skipped: {0}. - From bb277efaef4a635cdaba990a988ec165d96a2e93 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 8 Jan 2021 11:26:05 -0800 Subject: [PATCH 3/8] Address Ilya's feedback adding comment --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 17b7952a718..62eaebd69b5 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -293,6 +293,8 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex } catch (Exception e) when (e is ExtendedTypeSystemException || e is InvalidOperationException) { + // 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 if (formatErrorObject != null) { formatErrorObject.sourceObject = so; From 6a704b02282b462977707c0cc231e936974ae1b1 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 14 Jan 2021 14:26:58 -0800 Subject: [PATCH 4/8] add tracing when empty string is emitted due to exception --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 62eaebd69b5..77864df80ae 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -20,6 +20,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format /// internal static class PSObjectHelper { + #region tracer + [TraceSource("PSObjectHelper", "PSObjectHelper")] + internal static readonly PSTraceSource tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper"); + #endregion tracer + internal const char Ellipsis = '\u2026'; internal static string PSObjectIsOfExactType(Collection typeNames) @@ -295,6 +300,8 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex { // 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 + tracer.WriteLine("Exception during conversion to string, emitting empty string."); + if (formatErrorObject != null) { formatErrorObject.sourceObject = so; From 5d5125edc793bc470f2ff5944de93df91e7480cb Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 14 Jan 2021 14:29:36 -0800 Subject: [PATCH 5/8] address Codefactor issue --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 77864df80ae..1f7fd7913d9 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -22,7 +22,7 @@ internal static class PSObjectHelper { #region tracer [TraceSource("PSObjectHelper", "PSObjectHelper")] - internal static readonly PSTraceSource tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper"); + internal static readonly PSTraceSource Tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper"); #endregion tracer internal const char Ellipsis = '\u2026'; @@ -300,7 +300,7 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex { // 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 - tracer.WriteLine("Exception during conversion to string, emitting empty string."); + Tracer.WriteLine("Exception during conversion to string, emitting empty string."); if (formatErrorObject != null) { From 058b1788db3a39233ab9f777f4f0fef797ebc600 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 15 Jan 2021 15:27:52 -0800 Subject: [PATCH 6/8] Update src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs Co-authored-by: Ilya --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 1f7fd7913d9..b0a8167b508 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -22,7 +22,7 @@ internal static class PSObjectHelper { #region tracer [TraceSource("PSObjectHelper", "PSObjectHelper")] - internal static readonly PSTraceSource Tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper"); + private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper"); #endregion tracer internal const char Ellipsis = '\u2026'; From 352553e105aed386f27689e09d8a580315feb987 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 15 Jan 2021 15:28:00 -0800 Subject: [PATCH 7/8] Update src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs Co-authored-by: Ilya --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index b0a8167b508..6c4226db695 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -300,7 +300,7 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex { // 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 - Tracer.WriteLine("Exception during conversion to string, emitting empty string."); + s_tracer.TraceWarning("SmartToString method: Exception during conversion to string, emitting empty string."); if (formatErrorObject != null) { From f87d673620dc7a6575ac64c033bd0653f75f7e80 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 27 Jan 2021 10:28:11 -0800 Subject: [PATCH 8/8] address Rob's feedback to log the Exception message --- .../FormatAndOutput/common/Utilities/MshObjectUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index 6c4226db695..20fbe2fb99c 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -300,7 +300,7 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex { // 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."); + s_tracer.TraceWarning($"SmartToString method: Exception during conversion to string, emitting empty string: {e.Message}"); if (formatErrorObject != null) {