Skip to content

Commit

Permalink
chore: serizlize java Exception <==> javascript Error (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Aug 2, 2024
1 parent 46f4ac1 commit 776e3f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.options.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -172,6 +170,14 @@ private SerializedValue serializeValue(Object value) {
result.r = new SerializedValue.R();
result.r.p = ((Pattern)value).pattern();
result.r.f = toJsRegexFlags(((Pattern)value));
} else if (value instanceof Exception) {
Exception exception = (Exception) value;
result.e = new SerializedValue.E();
result.e.m = exception.getMessage();
result.e.n = exception.getClass().getSimpleName();
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
result.e.s = sw.toString();
} else {
HashableValue mapKey = new HashableValue(value);
Integer id = valueToId.get(mapKey);
Expand Down Expand Up @@ -250,9 +256,7 @@ private static <T> T deserialize(SerializedValue value, Map<Integer, Object> idT
if (value.r != null)
return (T)(Pattern.compile(value.r.p, fromJsRegexFlags(value.r.f)));
if (value.e != null) {
if (!value.e.s.isEmpty())
return (T)(value.e.s);
return (T)(value.e.m);
return (T)new Exception(value.e.s);
}
if (value.v != null) {
switch (value.v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,30 @@ void shouldWorkWithCSP() {

@Test
void shouldEvaluateException() {
String result = (String) page.evaluate("() => {\n" +
Exception result = (Exception) page.evaluate("() => {\n" +
" return (function functionOnStack() {\n" +
" return new Error('error message');\n" +
" })();\n" +
"}");
assertTrue(result.contains("Error: error message"), result);
assertTrue(result.contains("functionOnStack"), result);
assertTrue(result.getMessage().contains("Error: error message"), result.getMessage());
assertTrue(result.getMessage().contains("functionOnStack"), result.getMessage());
}

@Test
void shouldEvaluateException2() {
Object error = page.evaluate("new Error('error message')");
assertTrue(((String) error).contains("Error: error message"));
Exception error = (Exception) page.evaluate("new Error('error message')");
assertTrue(error.getMessage().contains("Error: error message"), error.getMessage());
}

@Test
void shouldPassExceptionArgument() {
Exception e = new Exception("error message");
Map<String, Object> received = (Map<String, Object>) page.evaluate("e => { return { message: e.message, name: e.name, stack: e.stack }; }", e);

assertEquals("error message", received.get("message"));
assertEquals("Exception", received.get("name"));
assertNotNull(received.get("stack"));
assertTrue(received.get("stack").toString().contains("shouldPassExceptionArgument"), received.get("stack").toString());
}

@Test
Expand Down

0 comments on commit 776e3f2

Please sign in to comment.