1
1
namespace Spectre . Console ;
2
2
3
3
// ExceptionFormatter relies heavily on reflection of types unknown until runtime.
4
- // We'll suppress these warnings, but alert the user this method is not supported.
5
4
[ UnconditionalSuppressMessage ( "AssemblyLoadTrimming" , "IL2026:RequiresUnreferencedCode" ) ]
6
5
[ UnconditionalSuppressMessage ( "AssemblyLoadTrimming" , "IL2070:RequiresUnreferencedCode" ) ]
7
6
[ UnconditionalSuppressMessage ( "AssemblyLoadTrimming" , "IL2075:RequiresUnreferencedCode" ) ]
8
- [ RequiresDynamicCode ( AotWarning ) ]
7
+ [ UnconditionalSuppressMessage ( "AssemblyLoadTrimming" , "IL3050:RequiresUnreferencedCode" ) ]
9
8
internal static class ExceptionFormatter
10
9
{
11
10
public const string AotWarning = "ExceptionFormatter is currently not supported for AOT." ;
@@ -27,13 +26,6 @@ private static IRenderable GetException(Exception exception, ExceptionSettings s
27
26
throw new ArgumentNullException ( nameof ( exception ) ) ;
28
27
}
29
28
30
- // fallback to default ToString() if someone in an AOT is insisting on using this method
31
- var stackTrace = new StackTrace ( exception , fNeedFileInfo : false ) ;
32
- if ( stackTrace . GetFrame ( 0 ) ? . GetMethod ( ) is null )
33
- {
34
- return new Text ( exception . ToString ( ) ) ;
35
- }
36
-
37
29
return new Rows ( GetMessage ( exception , settings ) , GetStackFrames ( exception , settings ) ) . Expand ( ) ;
38
30
}
39
31
@@ -71,8 +63,16 @@ private static Grid GetStackFrames(Exception ex, ExceptionSettings settings)
71
63
}
72
64
73
65
var stackTrace = new StackTrace ( ex , fNeedFileInfo : true ) ;
74
- var frames = stackTrace
75
- . GetFrames ( )
66
+ var allFrames = stackTrace . GetFrames ( ) ;
67
+ if ( allFrames [ 0 ] ? . GetMethod ( ) == null )
68
+ {
69
+ // if we can't easily get the method for the frame, then we are in AOT
70
+ // fallback to using ToString method of each frame.
71
+ WriteAotFrames ( grid , stackTrace . GetFrames ( ) , styles ) ;
72
+ return grid ;
73
+ }
74
+
75
+ var frames = allFrames
76
76
. FilterStackFrames ( )
77
77
. ToList ( ) ;
78
78
@@ -133,6 +133,23 @@ private static Grid GetStackFrames(Exception ex, ExceptionSettings settings)
133
133
return grid ;
134
134
}
135
135
136
+ private static void WriteAotFrames ( Grid grid , StackFrame ? [ ] frames , ExceptionStyle styles )
137
+ {
138
+ foreach ( var stackFrame in frames )
139
+ {
140
+ if ( stackFrame == null )
141
+ {
142
+ continue ;
143
+ }
144
+
145
+ var s = stackFrame . ToString ( ) ;
146
+ s = s . Replace ( " in file:line:column <filename unknown>:0:0" , string . Empty ) . TrimEnd ( ) ;
147
+ grid . AddRow (
148
+ $ "[{ styles . Dimmed . ToMarkup ( ) } ]at[/]",
149
+ s . EscapeMarkup ( ) ) ;
150
+ }
151
+ }
152
+
136
153
private static void AppendParameters ( StringBuilder builder , MethodBase ? method , ExceptionSettings settings )
137
154
{
138
155
var typeColor = settings . Style . ParameterType . ToMarkup ( ) ;
0 commit comments