Skip to content

Commit

Permalink
Fix possible NullPointerException in ErrorHandler.rebuildServerError
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik authored and barancev committed Oct 5, 2019
1 parent 893f66a commit bc1a581
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private <T extends Throwable> T createThrowable(

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

if (!rawErrorData.containsKey(CLASS) && !rawErrorData.containsKey(STACK_TRACE)) {
if (rawErrorData.get(CLASS) == null || rawErrorData.get(STACK_TRACE) == null) {
// Not enough information for us to try to rebuild an error.
return null;
}
Expand Down
54 changes: 54 additions & 0 deletions java/client/test/org/openqa/selenium/remote/ErrorHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,60 @@ public void testShouldStillTryToBuildWebDriverExceptionIfClassIsNotProvidedAndSt
assertStackTracesEqual(expectedTrace, cause.getStackTrace());
});
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassAndStackTraceIsNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", null,
"stackTrace", null);

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassNullAndStackTraceNotNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", null,
"stackTrace", "a");

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassNotNullAndStackTraceNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", "a",
"stackTrace", null);

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
}

@Test
public void testToleratesNonNumericLineNumber() {
Expand Down

0 comments on commit bc1a581

Please sign in to comment.