Skip to content

Commit 62676e9

Browse files
committed
Added eval exception handling
1 parent 1d3a6f7 commit 62676e9

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.togetherjava.jshellapi.dto;
2+
3+
public record JShellExceptionResult(String exceptionClass, String exceptionMessage) {
4+
}

JShellAPI/src/main/java/org/togetherjava/jshellapi/dto/JShellResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public record JShellResult(
88
int id,
99
String source,
1010
String result,
11+
JShellExceptionResult exception,
1112
boolean stdoutOverflow,
1213
String stdout,
1314
List<String> errors) {

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.togetherjava.jshellapi.service;
22

33
import org.apache.tomcat.util.http.fileupload.util.Closeable;
4+
import org.togetherjava.jshellapi.dto.JShellExceptionResult;
45
import org.togetherjava.jshellapi.dto.JShellResult;
56
import org.togetherjava.jshellapi.dto.SnippetStatus;
67
import org.togetherjava.jshellapi.dto.SnippetType;
@@ -91,14 +92,20 @@ public Optional<JShellResult> eval(String code) throws DockerException {
9192
String source = desanitize(reader.readLine());
9293
String result = reader.readLine();
9394
if(result.equals("NONE")) result = null;
95+
String rawException = reader.readLine();
96+
JShellExceptionResult exception = null;
97+
if(!rawException.isEmpty()) {
98+
String[] split = rawException.split(":");
99+
exception = new JShellExceptionResult(split[0], split[1]);
100+
}
94101
boolean stdoutOverflow = Boolean.parseBoolean(reader.readLine());
95102
String stdout = desanitize(reader.readLine());
96103
List<String> errors = new ArrayList<>();
97104
String error;
98105
while(!(error = reader.readLine()).isEmpty()) {
99106
errors.add(desanitize(error));
100107
}
101-
return Optional.of(new JShellResult(status, type, id, source, result, stdoutOverflow, stdout, errors));
108+
return Optional.of(new JShellResult(status, type, id, source, result, exception, stdoutOverflow, stdout, errors));
102109
} catch (IOException | NumberFormatException ex) {
103110
close();
104111
throw new DockerException(ex);

JShellWrapper/src/main/java/JShellWrapper.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import jdk.jshell.JShell;
2-
import jdk.jshell.Snippet;
3-
import jdk.jshell.SnippetEvent;
1+
import jdk.jshell.*;
42

53
import java.io.PrintStream;
64
import java.util.*;
@@ -55,6 +53,7 @@ private void ok() {
5553
* snippet id<br>
5654
* source<br>
5755
* result or NONE<br>
56+
* nothing or ExceptionClass:Exception message<br>
5857
* is stdout overflow<br>
5958
* stdout<br>
6059
* error 1<br>
@@ -89,6 +88,13 @@ private void eval(Scanner scanner, Config config, JShell shell, StringOutputStre
8988
result.add(event.snippet().id());
9089
result.add(sanitize(event.snippet().source()));
9190
result.add(event.value() != null ? event.value() : "NONE");
91+
if(event.exception() == null) {
92+
result.add("");
93+
} else if(event.exception() instanceof EvalException evalException) {
94+
result.add(sanitize(evalException.getExceptionClassName() + ":" + evalException.getMessage()));
95+
} else {
96+
result.add(sanitize(event.exception().getClass().getName() + ":" + event.exception().getMessage()));
97+
}
9298
result.add(String.valueOf(out.isOverflow()));
9399
result.add(sanitize(out.readAll()));
94100
if(event.status() == Snippet.Status.REJECTED) {

0 commit comments

Comments
 (0)