Skip to content

Commit 51739f2

Browse files
authored
fix NPE when response status is null (#17348)
I am not sure in which cases it happens, but I actually saw this error: ``` java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "org.openqa.selenium.remote.Response.getStatus()" is null at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:100) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:668) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:688) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:692) at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:507) at com.codeborne.selenide.drivercommands.CloseDriverCommand.quitSafely(CloseDriverCommand.java:50) at com.codeborne.selenide.drivercommands.CloseDriverCommand.close(CloseDriverCommand.java:35) at com.codeborne.selenide.impl.WebDriverInstance.dispose(WebDriverInstance.java:61) at java.base/java.util.concurrent.ConcurrentHashMap$ValuesView.forEach(ConcurrentHashMap.java:4783) at com.codeborne.selenide.drivercommands.DisposablesRegistry.disposeAllItems(DisposablesRegistry.java:57) at com.codeborne.selenide.drivercommands.DisposablesRegistry$SelenideCleanupShutdownHook.run(DisposablesRegistry.java:68) ```
1 parent e4382d0 commit 51739f2

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.remote;
1919

20+
import static java.util.Objects.requireNonNullElse;
21+
2022
import java.lang.reflect.Constructor;
2123
import java.math.BigDecimal;
2224
import java.math.RoundingMode;
@@ -97,8 +99,9 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
9799
throw new RuntimeException(throwable);
98100
}
99101

102+
int responseStatus = requireNonNullElse(response.getStatus(), -1);
100103
Class<? extends WebDriverException> outerErrorType =
101-
errorCodes.getExceptionType(response.getStatus());
104+
errorCodes.getExceptionType(responseStatus);
102105

103106
Object value = response.getValue();
104107
String message = null;
@@ -120,7 +123,7 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
120123
message = String.valueOf(e);
121124
}
122125

123-
Throwable serverError = rebuildServerError(rawErrorData, response.getStatus());
126+
Throwable serverError = rebuildServerError(rawErrorData, responseStatus);
124127

125128
// If serverError is null, then the server did not provide a className (only expected if
126129
// the server is a Java process) or a stack trace. The lack of a className is OK, but

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ void testShouldThrowAVanillaWebDriverExceptionIfServerDoesNotProvideAValue() {
104104
.withMessageContaining(new WebDriverException().getMessage());
105105
}
106106

107+
@Test
108+
void testShouldThrowAVanillaWebDriverExceptionIfResponseStatusIsNull() {
109+
Response response = new Response();
110+
assertThatExceptionOfType(WebDriverException.class)
111+
.isThrownBy(() -> handler.throwIfResponseFailed(response, 123))
112+
.withNoCause()
113+
.withMessageContaining(new WebDriverException().getMessage());
114+
}
115+
107116
@Test
108117
void testShouldNotSetCauseIfResponseValueIsJustAString() {
109118
assertThatExceptionOfType(WebDriverException.class)

0 commit comments

Comments
 (0)