@@ -263,13 +263,19 @@ private DynamicObject translateThrowable(Throwable throwable) {
263
263
264
264
final StringBuilder builder = new StringBuilder ();
265
265
boolean firstException = true ;
266
+ Backtrace lastBacktrace = null ;
266
267
267
268
while (t != null ) {
268
269
if (t .getClass ().getSimpleName ().equals ("LazyStackTrace" )) {
269
270
// Truffle's lazy stracktrace support, not a real exception
270
271
break ;
271
272
}
272
273
274
+ if (lastBacktrace != null ) {
275
+ appendTruffleStackTrace (builder , lastBacktrace );
276
+ lastBacktrace = null ;
277
+ }
278
+
273
279
if (!firstException ) {
274
280
builder .append ("Caused by:\n " );
275
281
}
@@ -290,15 +296,13 @@ private DynamicObject translateThrowable(Throwable throwable) {
290
296
builder .append (" (" ).append (t .getClass ().getSimpleName ()).append (")\n " );
291
297
292
298
if (t instanceof TruffleException ) {
293
- final Backtrace backtrace = new Backtrace ((TruffleException ) t );
294
- appendTruffleStackTrace (builder , backtrace );
299
+ lastBacktrace = new Backtrace ((TruffleException ) t );
295
300
} else {
301
+ // Print the first 10 lines of the Java stacktrace
302
+ appendJavaStackTrace (t , builder , 10 );
303
+
296
304
if (TruffleStackTrace .getStackTrace (t ) != null ) {
297
- final Backtrace backtrace = new Backtrace (t );
298
- appendTruffleStackTrace (builder , backtrace );
299
- } else {
300
- // Print the first 10 lines of the Java stacktrace
301
- appendJavaStackTrace (t , builder , 10 );
305
+ lastBacktrace = new Backtrace (t );
302
306
}
303
307
}
304
308
}
@@ -310,7 +314,11 @@ private DynamicObject translateThrowable(Throwable throwable) {
310
314
// When printing the backtrace of the exception, make it clear it's not a cause
311
315
builder .append ("Translated to internal error" );
312
316
313
- return coreExceptions ().runtimeError (builder .toString (), this , throwable );
317
+ if (lastBacktrace != null ) {
318
+ return coreExceptions ().runtimeError (builder .toString (), lastBacktrace );
319
+ } else {
320
+ return coreExceptions ().runtimeError (builder .toString (), this , throwable );
321
+ }
314
322
}
315
323
316
324
private void appendTruffleStackTrace (StringBuilder builder , Backtrace backtrace ) {
0 commit comments