Skip to content

Commit

Permalink
fix: Include traceback in Java Exceptions caused by Python Exceptions
Browse files Browse the repository at this point in the history
StringWriter should be used instead of ByteArrayOutputStream to capture
output.
  • Loading branch information
Christopher-Chianelli committed Sep 26, 2024
1 parent ec4c2af commit d48710e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package ai.timefold.jpyinterpreter.util;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

public class TracebackUtils {
private TracebackUtils() {

}

public static String getTraceback(Throwable t) {
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
PrintWriter printWriter = new PrintWriter(byteOutputStream);
var output = new StringWriter();
PrintWriter printWriter = new PrintWriter(output);
t.printStackTrace(printWriter);
return byteOutputStream.toString();
return output.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ai.timefold.jpyinterpreter.util;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class TracebackUtilsTest {
@Test
public void getTraceback() {
try {
throw new RuntimeException("A runtime error has occurred.");
} catch (RuntimeException e) {
assertThat(TracebackUtils.getTraceback(e))
.contains("A runtime error has occurred.")
.contains(TracebackUtilsTest.class.getCanonicalName());
}
}
}

0 comments on commit d48710e

Please sign in to comment.