Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 2b60161

Browse files
author
Clément Le Provost
committed
Fix null stream exception when reading an errored connection
Similar to #71 in the `v2` branch.
1 parent f5c9969 commit 2b60161

File tree

1 file changed

+8
-0
lines changed
  • algoliasearch/src/main/java/com/algolia/search/saas

1 file changed

+8
-0
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Client.java

+8
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ private byte[] _requestRaw(Method m, String url, String json, List<String> hosts
675675
final boolean codeIsError = code / 100 != 2;
676676
InputStream stream = codeIsError ?
677677
hostConnection.getErrorStream() : hostConnection.getInputStream();
678+
// As per the official Java docs (not the Android docs):
679+
// - `getErrorStream()` may return null => we have to handle this case.
680+
// See <https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getErrorStream()>.
681+
// - `getInputStream()` should never return null... but let's err on the side of caution.
682+
// See <https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getInputStream()>.
683+
if (stream == null) {
684+
throw new IOException(String.format("Null stream when reading connection (status %d)", code));
685+
}
678686

679687
final byte[] rawResponse;
680688
String encoding = hostConnection.getContentEncoding();

0 commit comments

Comments
 (0)