Skip to content

Commit 03d8d13

Browse files
committed
[java] Fixing compilation errors introduced by rebasing the previous commit and restoring backward compatibility.
1 parent bc1a581 commit 03d8d13

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

java/client/src/org/openqa/selenium/remote/ErrorHandler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
134134
message += " (WARNING: The client has suppressed server-side stacktraces)";
135135
} else {
136136
cause = serverError;
137+
if (cause.getStackTrace() == null || cause.getStackTrace().length == 0) {
138+
message += " (WARNING: The server did not provide any stacktrace information)";
139+
}
137140
}
138141

139142
if (rawErrorData.get(SCREEN_SHOT) != null) {
@@ -220,7 +223,7 @@ private <T extends Throwable> T createThrowable(
220223

221224
private Throwable rebuildServerError(Map<String, Object> rawErrorData, int responseStatus) {
222225

223-
if (rawErrorData.get(CLASS) == null || rawErrorData.get(STACK_TRACE) == null) {
226+
if (rawErrorData.get(CLASS) == null && rawErrorData.get(STACK_TRACE) == null) {
224227
// Not enough information for us to try to rebuild an error.
225228
return null;
226229
}
@@ -230,7 +233,7 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo
230233
Class<?> clazz = null;
231234

232235
// First: allow Remote Driver to specify the Selenium Server internal exception
233-
if (rawErrorData.containsKey(CLASS)) {
236+
if (rawErrorData.get(CLASS) != null) {
234237
String className = (String) rawErrorData.get(CLASS);
235238
try {
236239
clazz = Class.forName(className);
@@ -262,7 +265,7 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo
262265
// Note: if we have a class name above, we should always have a stack trace.
263266
// The inverse is not always true.
264267
StackTraceElement[] stackTrace = new StackTraceElement[0];
265-
if (rawErrorData.containsKey(STACK_TRACE)) {
268+
if (rawErrorData.get(STACK_TRACE) != null) {
266269
@SuppressWarnings({"unchecked"})
267270
List<Map<String, Object>> stackTraceInfo =
268271
(List<Map<String, Object>>) rawErrorData.get(STACK_TRACE);

java/client/test/org/openqa/selenium/remote/ErrorHandlerTest.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void testCauseShouldUseTheNamedClassIfAvailableOnTheClassPath() {
131131
.isThrownBy(() -> handler.throwIfResponseFailed(
132132
createResponse(ErrorCodes.UNHANDLED_ERROR,
133133
ImmutableMap.of("message", "boom", "class", NullPointerException.class.getName())), 123))
134-
.withMessage(new WebDriverException("boom\nCommand duration or timeout: 123 milliseconds").getMessage())
134+
.withMessage(new WebDriverException("boom (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds").getMessage())
135135
.withCauseInstanceOf(NullPointerException.class)
136136
.satisfies(expected -> assertThat(expected.getCause()).hasMessage("boom"));
137137
}
@@ -142,7 +142,7 @@ public void testCauseStackTraceShouldBeEmptyIfTheServerDidNotProvideThatInformat
142142
.isThrownBy(() -> handler.throwIfResponseFailed(
143143
createResponse(ErrorCodes.UNHANDLED_ERROR,
144144
ImmutableMap.of("message", "boom", "class", NullPointerException.class.getName())), 1234))
145-
.withMessage(new WebDriverException("boom\nCommand duration or timeout: 1.23 seconds").getMessage())
145+
.withMessage(new WebDriverException("boom (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 1.23 seconds").getMessage())
146146
.withCauseInstanceOf(NullPointerException.class)
147147
.satisfies(expected -> {
148148
assertThat(expected.getCause()).hasMessage("boom");
@@ -239,58 +239,53 @@ public void testShouldStillTryToBuildWebDriverExceptionIfClassIsNotProvidedAndSt
239239
});
240240
}
241241

242-
@SuppressWarnings("ThrowableInstanceNeverThrown")
243242
@Test
244243
public void testShoulNotBuildWebDriverExceptionIfClassAndStackTraceIsNull() {
245-
Map<String, ?> data = ImmutableMap.of(
246-
"message", "some error message",
247-
"class", null,
248-
"stackTrace", null);
249-
250-
try {
251-
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
252-
fail("Should have thrown!");
253-
} catch (WebDriverException expected) {
254-
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
255-
new WebDriverException()).getMessage(),
256-
expected.getMessage());
257-
}
244+
Map<String, Object> data = new HashMap<>();
245+
data.put("message", "some error message");
246+
data.put("class", null);
247+
data.put("stackTrace", null);
248+
249+
assertThatExceptionOfType(WebDriverException.class)
250+
.isThrownBy(() -> handler.throwIfResponseFailed(
251+
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
252+
.withMessageStartingWith(new WebDriverException(
253+
"some error message (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds",
254+
new WebDriverException()).getMessage());
258255
}
259256

260-
@SuppressWarnings("ThrowableInstanceNeverThrown")
261257
@Test
262258
public void testShoulNotBuildWebDriverExceptionIfClassNullAndStackTraceNotNull() {
263-
Map<String, ?> data = ImmutableMap.of(
264-
"message", "some error message",
265-
"class", null,
266-
"stackTrace", "a");
267-
268-
try {
269-
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
270-
fail("Should have thrown!");
271-
} catch (WebDriverException expected) {
272-
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
273-
new WebDriverException()).getMessage(),
274-
expected.getMessage());
275-
}
259+
Map<String, Object> data = new HashMap<>();
260+
data.put("message", "some error message");
261+
data.put("class", null);
262+
data.put("stackTrace", Collections.singletonList(
263+
ImmutableMap.of("lineNumber", 1224,
264+
"methodName", "someMethod",
265+
"className", "MyClass",
266+
"fileName", "Resource.m")));
267+
268+
assertThatExceptionOfType(WebDriverException.class)
269+
.isThrownBy(() -> handler.throwIfResponseFailed(
270+
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
271+
.withMessageStartingWith(new WebDriverException(
272+
"some error message\nCommand duration or timeout: 123 milliseconds",
273+
new WebDriverException()).getMessage());
276274
}
277275

278-
@SuppressWarnings("ThrowableInstanceNeverThrown")
279276
@Test
280277
public void testShoulNotBuildWebDriverExceptionIfClassNotNullAndStackTraceNull() {
281-
Map<String, ?> data = ImmutableMap.of(
282-
"message", "some error message",
283-
"class", "a",
284-
"stackTrace", null);
285-
286-
try {
287-
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
288-
fail("Should have thrown!");
289-
} catch (WebDriverException expected) {
290-
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
291-
new WebDriverException()).getMessage(),
292-
expected.getMessage());
293-
}
278+
Map<String, Object> data = new HashMap<>();
279+
data.put("message", "some error message");
280+
data.put("class", "a");
281+
data.put("stackTrace", null);
282+
283+
assertThatExceptionOfType(WebDriverException.class)
284+
.isThrownBy(() -> handler.throwIfResponseFailed(
285+
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
286+
.withMessageStartingWith(new WebDriverException(
287+
"some error message (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds",
288+
new WebDriverException()).getMessage());
294289
}
295290

296291
@Test

0 commit comments

Comments
 (0)