-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Include traceback in Java Exceptions caused by Python Exceptions
StringWriter should be used instead of ByteArrayOutputStream to capture output.
- Loading branch information
1 parent
ec4c2af
commit d48710e
Showing
2 changed files
with
22 additions
and
4 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
python/jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/util/TracebackUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
python/jpyinterpreter/src/test/java/ai/timefold/jpyinterpreter/util/TracebackUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |