Skip to content

Commit 1927cee

Browse files
committed
Treat the the last Backtrace as the Ruby exception's Backtrace
* Show the Java-level stacktrace for non-TruffleException to help debugging
1 parent 408c3d8 commit 1927cee

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ public DynamicObject runtimeError(String fullMessage, Node currentNode, Throwabl
246246
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, currentNode, javaThrowable);
247247
}
248248

249+
@TruffleBoundary
250+
public DynamicObject runtimeError(String fullMessage, Backtrace backtrace) {
251+
DynamicObject exceptionClass = context.getCoreLibrary().getRuntimeErrorClass();
252+
DynamicObject errorMessage = StringOperations.createString(context, StringOperations.encodeRope(fullMessage, UTF8Encoding.INSTANCE));
253+
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, backtrace);
254+
}
255+
249256
// SystemStackError
250257

251258
@TruffleBoundary

src/main/java/org/truffleruby/core/exception/ExceptionOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static DynamicObject createSystemCallError(RubyContext context, DynamicOb
7171

7272
// because the factory is not constant
7373
@TruffleBoundary
74-
private static DynamicObject createRubyException(RubyContext context, DynamicObject rubyClass, Object message, Backtrace backtrace) {
74+
public static DynamicObject createRubyException(RubyContext context, DynamicObject rubyClass, Object message, Backtrace backtrace) {
7575
final DynamicObject cause = ThreadGetExceptionNode.getLastException(context);
7676
context.getCoreExceptions().showExceptionIfDebug(rubyClass, message, backtrace);
7777
return Layouts.CLASS.getInstanceFactory(rubyClass).newInstance(Layouts.EXCEPTION.build(message, null, backtrace, cause));

src/main/java/org/truffleruby/language/methods/ExceptionTranslatingNode.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,19 @@ private DynamicObject translateThrowable(Throwable throwable) {
263263

264264
final StringBuilder builder = new StringBuilder();
265265
boolean firstException = true;
266+
Backtrace lastBacktrace = null;
266267

267268
while (t != null) {
268269
if (t.getClass().getSimpleName().equals("LazyStackTrace")) {
269270
// Truffle's lazy stracktrace support, not a real exception
270271
break;
271272
}
272273

274+
if (lastBacktrace != null) {
275+
appendTruffleStackTrace(builder, lastBacktrace);
276+
lastBacktrace = null;
277+
}
278+
273279
if (!firstException) {
274280
builder.append("Caused by:\n");
275281
}
@@ -290,15 +296,13 @@ private DynamicObject translateThrowable(Throwable throwable) {
290296
builder.append(" (").append(t.getClass().getSimpleName()).append(")\n");
291297

292298
if (t instanceof TruffleException) {
293-
final Backtrace backtrace = new Backtrace((TruffleException) t);
294-
appendTruffleStackTrace(builder, backtrace);
299+
lastBacktrace = new Backtrace((TruffleException) t);
295300
} else {
301+
// Print the first 10 lines of the Java stacktrace
302+
appendJavaStackTrace(t, builder, 10);
303+
296304
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);
302306
}
303307
}
304308
}
@@ -310,7 +314,11 @@ private DynamicObject translateThrowable(Throwable throwable) {
310314
// When printing the backtrace of the exception, make it clear it's not a cause
311315
builder.append("Translated to internal error");
312316

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+
}
314322
}
315323

316324
private void appendTruffleStackTrace(StringBuilder builder, Backtrace backtrace) {

0 commit comments

Comments
 (0)