Commit cdf26a01 by claincly Committed by bachinger

Remove error code inference in DataSourceException.

The inference is used when nesting DataSourceExceptions. It is removed because
nesting does not add additional value in surfacing the exceptions, and it is
better to assign an error code at the throw site (in the "leaf" or the bottom
most data source).

PiperOrigin-RevId: 386919118
parent 873e83c5
...@@ -18,8 +18,6 @@ package com.google.android.exoplayer2.upstream; ...@@ -18,8 +18,6 @@ package com.google.android.exoplayer2.upstream;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
import java.io.IOException; import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/** Used to specify reason of a DataSource error. */ /** Used to specify reason of a DataSource error. */
public class DataSourceException extends IOException { public class DataSourceException extends IOException {
...@@ -80,7 +78,7 @@ public class DataSourceException extends IOException { ...@@ -80,7 +78,7 @@ public class DataSourceException extends IOException {
public DataSourceException( public DataSourceException(
String message, Throwable cause, @PlaybackException.ErrorCode int reason) { String message, Throwable cause, @PlaybackException.ErrorCode int reason) {
super(message, cause); super(message, cause);
this.reason = inferErrorCode(reason, cause); this.reason = reason;
} }
/** /**
...@@ -92,7 +90,7 @@ public class DataSourceException extends IOException { ...@@ -92,7 +90,7 @@ public class DataSourceException extends IOException {
*/ */
public DataSourceException(Throwable cause, @PlaybackException.ErrorCode int reason) { public DataSourceException(Throwable cause, @PlaybackException.ErrorCode int reason) {
super(cause); super(cause);
this.reason = inferErrorCode(reason, cause); this.reason = reason;
} }
/** /**
...@@ -106,24 +104,4 @@ public class DataSourceException extends IOException { ...@@ -106,24 +104,4 @@ public class DataSourceException extends IOException {
super(message); super(message);
this.reason = reason; this.reason = reason;
} }
@PlaybackException.ErrorCode
private static int inferErrorCode(
@PlaybackException.ErrorCode int reason, @Nullable Throwable cause) {
if (reason != PlaybackException.ERROR_CODE_IO_UNSPECIFIED) {
return reason;
}
while (cause != null) {
if (cause instanceof UnknownHostException) {
return PlaybackException.ERROR_CODE_IO_DNS_FAILED;
} else if (cause instanceof SocketTimeoutException) {
return PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT;
} else if (cause instanceof DataSourceException) {
return ((DataSourceException) cause).reason;
}
cause = cause.getCause();
}
return PlaybackException.ERROR_CODE_IO_UNSPECIFIED;
}
} }
...@@ -17,12 +17,9 @@ package com.google.android.exoplayer2.upstream; ...@@ -17,12 +17,9 @@ package com.google.android.exoplayer2.upstream;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
import java.io.IOException; import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -58,68 +55,4 @@ public class DataSourceExceptionTest { ...@@ -58,68 +55,4 @@ public class DataSourceExceptionTest {
IOException e = new IOException(new IOException(cause)); IOException e = new IOException(new IOException(cause));
assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse(); assertThat(DataSourceException.isCausedByPositionOutOfRange(e)).isFalse();
} }
@Test
public void constructor_withNestedCausesAndUnspecifiedErrorCodes_assignsCorrectErrorCodes() {
DataSourceException exception =
new DataSourceException(
new UnknownHostException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason).isEqualTo(PlaybackException.ERROR_CODE_IO_DNS_FAILED);
exception =
new DataSourceException(
new SocketTimeoutException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason)
.isEqualTo(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT);
exception =
new DataSourceException(
new IOException(new SocketTimeoutException()),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason)
.isEqualTo(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT);
exception =
new DataSourceException(
new DataSourceException(
new SocketTimeoutException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason)
.isEqualTo(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT);
exception =
new DataSourceException(
new DataSourceException(
new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason)
.isEqualTo(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE);
exception =
new DataSourceException(
new HttpDataSource.CleartextNotPermittedException(
new IOException(), new DataSpec(Uri.parse("test"))),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason).isEqualTo(PlaybackException.ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED);
exception =
new DataSourceException(
new HttpDataSource.HttpDataSourceException(
new IOException(),
new DataSpec(Uri.parse("test")),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED,
HttpDataSource.HttpDataSourceException.TYPE_OPEN),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason)
.isEqualTo(PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED);
exception =
new DataSourceException(
new DataSourceException(
new DataSourceException(PlaybackException.ERROR_CODE_IO_UNSPECIFIED),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED),
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
assertThat(exception.reason).isEqualTo(PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment