Skip to content

Commit 7873dc1

Browse files
authored
Fix NullPointerException when reading error response body (#276)
## Changes Fix NullPointerException when reading error response body Fixes the following exception ``` Caused by: java.lang.NullPointerException at java.io.Reader.<init>(Reader.java:78) at java.io.InputStreamReader.<init>(InputStreamReader.java:113) at org.apache.commons.io.IOUtils.copy(IOUtils.java:921) at org.apache.commons.io.IOUtils.toString(IOUtils.java:2681) at com.databricks.sdk.core.error.ApiErrors.parseApiError(ApiErrors.java:70) at com.databricks.sdk.core.error.ApiErrors.readErrorFromResponse(ApiErrors.java:34) at com.databricks.sdk.core.error.ApiErrors.checkForRetry(ApiErrors.java:26) ... ```
1 parent 4d830b5 commit 7873dc1

File tree

1 file changed

+9
-1
lines changed
  • databricks-sdk-java/src/main/java/com/databricks/sdk/core/error

1 file changed

+9
-1
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/error/ApiErrors.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ private static DatabricksError readErrorFromResponse(Response response) {
6262
*/
6363
private static ApiErrorBody parseApiError(Response response) {
6464
try {
65+
InputStream in = response.getBody();
66+
if (in == null) {
67+
ApiErrorBody errorBody = new ApiErrorBody();
68+
errorBody.setMessage(
69+
String.format("Status response from server: %s", response.getStatus()));
70+
return errorBody;
71+
}
72+
6573
// Read the body now, so we can try to parse as JSON and then fallback to old error handling
6674
// logic.
67-
String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
75+
String body = IOUtils.toString(in, StandardCharsets.UTF_8);
6876
try {
6977
return MAPPER.readValue(body, ApiErrorBody.class);
7078
} catch (IOException e) {

0 commit comments

Comments
 (0)