@@ -13,7 +13,6 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell
1313 internal class ErrorRecordFormatter
1414 {
1515 private const string TruncationPostfix = "..." ;
16- private const string Indent = " " ;
1716
1817 private readonly PowerShell _pwsh = PowerShell . Create ( ) ;
1918
@@ -26,89 +25,23 @@ internal class ErrorRecordFormatter
2625 /// </summary>
2726 public string Format ( ErrorRecord errorRecord , int maxSize = 1 * 1024 * 1024 )
2827 {
29- _pwsh . AddScript ( "$ErrorView = 'NormalView'" ) . InvokeAndClearCommands ( ) ;
30- var errorDetails = _pwsh . AddCommand ( "Microsoft.PowerShell.Utility\\ Out-String" )
28+ var errorDetails = _pwsh . AddCommand ( "Microsoft.PowerShell.Utility\\ Get-Error" )
3129 . AddParameter ( "InputObject" , errorRecord )
30+ . AddCommand ( "Microsoft.PowerShell.Utility\\ Out-String" )
3231 . InvokeAndClearCommands < string > ( ) ;
3332
34- var result = new StringBuilder (
35- capacity : Math . Min ( 1024 , maxSize ) ,
36- maxCapacity : maxSize ) ;
33+ var result = new StringBuilder ( capacity : Math . Min ( 1024 , maxSize ) ) ;
3734
38- try
39- {
40- result . Append ( errorDetails . Single ( ) ) ;
41- result . AppendLine ( "Script stack trace:" ) ;
42- AppendStackTrace ( result , errorRecord . ScriptStackTrace , Indent ) ;
43- result . AppendLine ( ) ;
44-
45- if ( errorRecord . Exception != null )
46- {
47- AppendExceptionWithInners ( result , errorRecord . Exception ) ;
48- }
49-
50- return result . ToString ( ) ;
51- }
52- catch ( ArgumentOutOfRangeException ) // exceeding StringBuilder max capacity
53- {
54- return Truncate ( result , maxSize ) ;
55- }
56- }
57-
58- private static void AppendExceptionWithInners ( StringBuilder result , Exception exception )
59- {
60- AppendExceptionInfo ( result , exception ) ;
61-
62- if ( exception is AggregateException aggregateException )
63- {
64- foreach ( var innerException in aggregateException . Flatten ( ) . InnerExceptions )
65- {
66- AppendInnerExceptionIfNotNull ( result , innerException ) ;
67- }
68- }
69- else
70- {
71- AppendInnerExceptionIfNotNull ( result , exception . InnerException ) ;
72- }
73- }
74-
75- private static void AppendInnerExceptionIfNotNull ( StringBuilder result , Exception innerException )
76- {
77- if ( innerException != null )
78- {
79- result . Append ( "Inner exception: " ) ;
80- AppendExceptionWithInners ( result , innerException ) ;
81- }
82- }
83-
84- private static void AppendExceptionInfo ( StringBuilder stringBuilder , Exception exception )
85- {
86- stringBuilder . Append ( exception . GetType ( ) . FullName ) ;
87- stringBuilder . Append ( ": " ) ;
88- stringBuilder . AppendLine ( exception . Message ) ;
89-
90- AppendStackTrace ( stringBuilder , exception . StackTrace , string . Empty ) ;
91- stringBuilder . AppendLine ( ) ;
92- }
93-
94- private static void AppendStackTrace ( StringBuilder stringBuilder , string stackTrace , string indent )
95- {
96- if ( stackTrace != null )
97- {
98- stringBuilder . Append ( indent ) ;
99- stringBuilder . AppendLine ( stackTrace . Replace ( Environment . NewLine , Environment . NewLine + indent ) ) ;
100- }
101- }
102-
103- private static string Truncate ( StringBuilder result , int maxSize )
104- {
105- var charactersToRemove = result . Length + TruncationPostfix . Length - maxSize ;
106- if ( charactersToRemove > 0 )
35+ result . AppendLine ( errorRecord . Exception . Message ) ;
36+ result . Append ( errorDetails . Single ( ) ) ;
37+ if ( result . Length > maxSize )
10738 {
39+ var charactersToRemove = result . Length + TruncationPostfix . Length - maxSize ;
10840 result . Remove ( result . Length - charactersToRemove , charactersToRemove ) ;
41+ result . Append ( TruncationPostfix ) ;
10942 }
11043
111- return result + TruncationPostfix ;
44+ return result . ToString ( ) ;
11245 }
11346 }
11447}
0 commit comments