Skip to content

Commit 6598631

Browse files
committed
Get the backtrace of TruffleException in ExceptionTranslatingNode
1 parent 46466ee commit 6598631

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/main/java/org/truffleruby/language/backtrace/Backtrace.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public Backtrace(Node location, SourceSection sourceLocation, int omitted, Throw
4545
this.javaThrowable = javaThrowable;
4646
}
4747

48+
public Backtrace(TruffleException exception) {
49+
this.location = exception.getLocation();
50+
this.sourceLocation = exception.getSourceLocation();
51+
this.omitted = 0;
52+
this.javaThrowable = null;
53+
54+
this.activations = getActivations(exception);
55+
}
56+
4857
public Backtrace copy(RubyContext context, DynamicObject exception) {
4958
Backtrace copy = new Backtrace(location, sourceLocation, omitted, javaThrowable);
5059
// A Backtrace is 1-1-1 with a RaiseException and a Ruby exception

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.truffleruby.Layouts;
2020
import org.truffleruby.language.RubyGuards;
2121
import org.truffleruby.language.RubyNode;
22+
import org.truffleruby.language.backtrace.Backtrace;
2223
import org.truffleruby.language.backtrace.BacktraceFormatter;
2324
import org.truffleruby.language.backtrace.BacktraceFormatter.FormattingFlags;
2425
import org.truffleruby.language.control.JavaException;
@@ -285,13 +286,19 @@ private DynamicObject translateThrowable(Throwable throwable) {
285286
// Java exception, print it formatted like a Ruby exception
286287
final String message = t.getMessage();
287288
builder.append(message != null ? message : "<no message>");
288-
289289
builder.append(" (").append(t.getClass().getSimpleName()).append(")\n");
290290

291-
// Print the first 10 lines of backtrace
292-
final StackTraceElement[] stackTrace = t.getStackTrace();
293-
for (int i = 0; i < Math.min(stackTrace.length, 10); i++) {
294-
stackTraceElementToRuby(builder, stackTrace[i]);
291+
if (t instanceof TruffleException) {
292+
final Backtrace backtrace = new Backtrace((TruffleException) t);
293+
final BacktraceFormatter formatter = new BacktraceFormatter(getContext(), EnumSet.noneOf(FormattingFlags.class));
294+
final String formattedBacktrace = formatter.formatBacktrace(null, backtrace);
295+
builder.append(formattedBacktrace).append('\n');
296+
} else {
297+
// Print the first 10 lines of backtrace
298+
final StackTraceElement[] stackTrace = t.getStackTrace();
299+
for (int i = 0; i < Math.min(stackTrace.length, 10); i++) {
300+
stackTraceElementToRuby(builder, stackTrace[i]);
301+
}
295302
}
296303
}
297304

0 commit comments

Comments
 (0)