@@ -13,7 +13,6 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell
13
13
internal class ErrorRecordFormatter
14
14
{
15
15
private const string TruncationPostfix = "..." ;
16
- private const string Indent = " " ;
17
16
18
17
private readonly PowerShell _pwsh = PowerShell . Create ( ) ;
19
18
@@ -26,89 +25,23 @@ internal class ErrorRecordFormatter
26
25
/// </summary>
27
26
public string Format ( ErrorRecord errorRecord , int maxSize = 1 * 1024 * 1024 )
28
27
{
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" )
31
29
. AddParameter ( "InputObject" , errorRecord )
30
+ . AddCommand ( "Microsoft.PowerShell.Utility\\ Out-String" )
32
31
. InvokeAndClearCommands < string > ( ) ;
33
32
34
- var result = new StringBuilder (
35
- capacity : Math . Min ( 1024 , maxSize ) ,
36
- maxCapacity : maxSize ) ;
33
+ var result = new StringBuilder ( capacity : Math . Min ( 1024 , maxSize ) ) ;
37
34
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 )
107
38
{
39
+ var charactersToRemove = result . Length + TruncationPostfix . Length - maxSize ;
108
40
result . Remove ( result . Length - charactersToRemove , charactersToRemove ) ;
41
+ result . Append ( TruncationPostfix ) ;
109
42
}
110
43
111
- return result + TruncationPostfix ;
44
+ return result . ToString ( ) ;
112
45
}
113
46
}
114
47
}
0 commit comments