Update OkHttpDataSource with response body on bad HTTP status

This change supplies the response body in OkHttpDataSource to
InvalidResponseCodeException.

Issue #6853
This commit is contained in:
Fillmore, Christopher 2020-01-14 13:48:14 -05:00
parent d94a40ac77
commit 78740bcac6

View File

@ -230,10 +230,19 @@ public class OkHttpDataSource extends BaseDataSource implements HttpDataSource {
// Check for a valid response code.
if (!response.isSuccessful()) {
byte[] errorResponseBody;
try {
errorResponseBody = Util.toByteArray(responseByteStream);
} catch (IOException e) {
throw new HttpDataSourceException(
"Error reading response body of unsuccessful response " + dataSpec.uri,
e, dataSpec, HttpDataSourceException.TYPE_OPEN);
}
Map<String, List<String>> headers = response.headers().toMultimap();
closeConnectionQuietly();
InvalidResponseCodeException exception =
new InvalidResponseCodeException(responseCode, response.message(), headers, dataSpec);
new InvalidResponseCodeException(responseCode, response.message(), headers, dataSpec,
errorResponseBody);
if (responseCode == 416) {
exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
}