9
9
*/
10
10
package org .truffleruby .language .exceptions ;
11
11
12
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
13
- import com .oracle .truffle .api .TruffleStackTrace ;
14
- import com .oracle .truffle .api .exception .AbstractTruffleException ;
15
12
import org .truffleruby .core .exception .RubyException ;
16
13
import org .truffleruby .core .exception .RubySystemExit ;
17
14
import org .truffleruby .core .kernel .AtExitManager ;
18
15
import org .truffleruby .core .thread .GetCurrentRubyThreadNode ;
19
16
import org .truffleruby .language .RubyContextNode ;
20
- import org .truffleruby .language .backtrace .Backtrace ;
21
17
import org .truffleruby .language .backtrace .BacktraceFormatter ;
22
- import org .truffleruby .language .backtrace .BacktraceInterleaver ;
23
18
import org .truffleruby .language .control .ExitException ;
24
19
import org .truffleruby .language .control .RaiseException ;
25
20
import org .truffleruby .language .dispatch .DispatchNode ;
26
21
27
22
import com .oracle .truffle .api .CompilerDirectives ;
28
23
29
- import java .io .PrintStream ;
30
- import java .util .EnumSet ;
31
-
32
24
public class TopLevelRaiseHandler extends RubyContextNode {
33
25
34
26
@ Child private GetCurrentRubyThreadNode getCurrentRubyThreadNode ;
@@ -49,7 +41,10 @@ public int execute(Runnable body) {
49
41
// hard #exit!, return immediately, skip at_exit hooks
50
42
return e .getCode ();
51
43
} catch (RuntimeException | Error e ) {
52
- printInternalError (e );
44
+ BacktraceFormatter .printInternalError (
45
+ getContext (),
46
+ e ,
47
+ "an internal exception escaped out of the interpreter" );
53
48
return 1 ;
54
49
}
55
50
@@ -72,7 +67,10 @@ public int execute(Runnable body) {
72
67
// hard #exit! during at_exit: ignore the main script exception
73
68
exitCode = e .getCode ();
74
69
} catch (RuntimeException | Error e ) { // Internal error
75
- printInternalError (e );
70
+ BacktraceFormatter .printInternalError (
71
+ getContext (),
72
+ e ,
73
+ "an internal exception escaped out of the interpreter" );
76
74
return 1 ;
77
75
}
78
76
@@ -103,78 +101,4 @@ private void handleSignalException(RubyException exception) {
103
101
}
104
102
}
105
103
106
- @ TruffleBoundary
107
- private void printInternalError (Throwable throwable ) {
108
- final PrintStream stream = BacktraceFormatter .printStreamFor (getContext ().getEnv ().err ());
109
- stream .println ();
110
- stream .println ("truffleruby: an internal exception escaped out of the interpreter," );
111
- stream .println ("please report it to https://github.com/oracle/truffleruby/issues." );
112
- stream .println ();
113
- stream .println ("```" );
114
-
115
- boolean firstException = true ;
116
- Throwable t = throwable ;
117
-
118
- while (t != null ) {
119
- if (t .getClass ().getSimpleName ().equals ("LazyStackTrace" )) {
120
- // Truffle's lazy stracktrace support, not a real exception
121
- break ;
122
- }
123
-
124
- if (!firstException ) {
125
- stream .println ("Caused by:" );
126
- }
127
-
128
- if (t instanceof RaiseException ) {
129
- // A Ruby exception as a cause of a Java or C-ext exception
130
- final RubyException rubyException = ((RaiseException ) t ).getException ();
131
-
132
- final BacktraceFormatter formatter = new BacktraceFormatter (
133
- getContext (),
134
- getLanguage (),
135
- EnumSet .noneOf (BacktraceFormatter .FormattingFlags .class ));
136
- final String formattedBacktrace = formatter
137
- .formatBacktrace (rubyException , rubyException .backtrace );
138
- stream .println (formattedBacktrace );
139
- } else {
140
- stream .println (BacktraceFormatter .formatJavaThrowableMessage (t ));
141
-
142
- if (t instanceof AbstractTruffleException ) {
143
- // Foreign exception
144
- printTruffleStackTrace (stream , new Backtrace ((AbstractTruffleException ) t ));
145
- } else {
146
- // Internal error, print it formatted like a Ruby exception
147
- printJavaStackTrace (stream , t );
148
-
149
- if (TruffleStackTrace .getStackTrace (t ) != null ) {
150
- printTruffleStackTrace (stream , new Backtrace (t ));
151
- }
152
- }
153
- }
154
-
155
- t = t .getCause ();
156
- firstException = false ;
157
- }
158
-
159
- stream .println ("```" );
160
- }
161
-
162
- private void printTruffleStackTrace (PrintStream stream , Backtrace backtrace ) {
163
- final BacktraceFormatter formatter = new BacktraceFormatter (
164
- getContext (),
165
- getLanguage (),
166
- EnumSet .noneOf (BacktraceFormatter .FormattingFlags .class ));
167
- stream .println (formatter .formatBacktrace (null , backtrace ));
168
- }
169
-
170
- private void printJavaStackTrace (PrintStream stream , Throwable t ) {
171
- final StackTraceElement [] stackTrace = t .getStackTrace ();
172
- for (StackTraceElement stackTraceElement : stackTrace ) {
173
- stream .println ("\t from " + stackTraceElement );
174
- if (BacktraceInterleaver .isCallBoundary (stackTraceElement )) {
175
- break ;
176
- }
177
- }
178
- }
179
-
180
104
}
0 commit comments