From 5bc1c4837ff5548c6db0222156916ca03464c8b2 Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Wed, 28 Jul 2021 10:10:14 +0100 Subject: [PATCH] Assign CronetDataSource error codes PiperOrigin-RevId: 387301144 --- .../ext/cronet/CronetDataSource.java | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java b/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java index 33e29013b5..ad795b7051 100644 --- a/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java +++ b/extensions/cronet/src/main/java/com/google/android/exoplayer2/ext/cronet/CronetDataSource.java @@ -669,11 +669,12 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { if (message != null && Ascii.toLowerCase(message).contains("err_cleartext_not_permitted")) { throw new CleartextNotPermittedException(connectionOpenException, dataSpec); } + @PlaybackException.ErrorCode + int errorCode = + getErrorCodeForException( + connectionOpenException, /* occurredWhileOpeningConnection*/ true); throw new OpenException( - connectionOpenException, - dataSpec, - PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, - getStatus(urlRequest)); + connectionOpenException, dataSpec, errorCode, getStatus(urlRequest)); } else if (!connectionOpened) { // The timeout was reached before the connection was opened. throw new OpenException( @@ -1026,7 +1027,10 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { throw (HttpDataSourceException) e; } else { throw new OpenException( - e, dataSpec, PlaybackException.ERROR_CODE_IO_UNSPECIFIED, Status.READING_RESPONSE); + e, + dataSpec, + getErrorCodeForException(e, /* occurredWhileOpeningConnection= */ false), + Status.READING_RESPONSE); } } } @@ -1098,9 +1102,7 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { throw new HttpDataSourceException( exception, dataSpec, - exception instanceof UnknownHostException - ? PlaybackException.ERROR_CODE_IO_DNS_FAILED - : PlaybackException.ERROR_CODE_IO_UNSPECIFIED, + getErrorCodeForException(exception, /* occurredWhileOpeningConnection= */ false), HttpDataSourceException.TYPE_READ); } } @@ -1114,6 +1116,25 @@ public class CronetDataSource extends BaseDataSource implements HttpDataSource { return readBuffer; } + @PlaybackException.ErrorCode + private static int getErrorCodeForException( + IOException exception, boolean occurredWhileOpeningConnection) { + if (exception instanceof UnknownHostException) { + return PlaybackException.ERROR_CODE_IO_DNS_FAILED; + } + @Nullable String message = exception.getMessage(); + if (message != null) { + if (message.contains("net::ERR_INTERNET_DISCONNECTED")) { + return PlaybackException.ERROR_CODE_IO_NETWORK_UNAVAILABLE; + } + } + if (occurredWhileOpeningConnection) { + return PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED; + } else { + return PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_CLOSED; + } + } + private static boolean isCompressed(UrlResponseInfo info) { for (Map.Entry entry : info.getAllHeadersAsList()) { if (entry.getKey().equalsIgnoreCase("Content-Encoding")) {