Skip to content

Commit aa28a85

Browse files
committed
Added a workaround for not serializable exceptions
1 parent f18ee8e commit aa28a85

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.schabi.newpipe.error;
2+
3+
import android.util.Log;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.io.ObjectOutputStream;
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
/**
15+
* Ensures that a Exception is serializable.
16+
* This is
17+
*/
18+
public final class EnsureExceptionSerializable {
19+
private static final String TAG = "EnsureExSerializable";
20+
21+
private EnsureExceptionSerializable() {
22+
// No instance
23+
}
24+
25+
/**
26+
* Ensures that an exception is serializable.
27+
* <br/>
28+
* If that is not the case a {@link WorkaroundNotSerializableException} is created.
29+
*
30+
* @param exception
31+
* @return if an exception is not serializable a new {@link WorkaroundNotSerializableException}
32+
* otherwise the exception from the parameter
33+
*/
34+
public static Exception ensureSerializable(@NonNull final Exception exception) {
35+
return checkIfSerializable(exception)
36+
? exception
37+
: WorkaroundNotSerializableException.create(exception);
38+
}
39+
40+
public static boolean checkIfSerializable(@NonNull final Exception exception) {
41+
try {
42+
// Check by creating a new ObjectOutputStream which does the serialization
43+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
44+
ObjectOutputStream oos = new ObjectOutputStream(bos)
45+
) {
46+
oos.writeObject(exception);
47+
oos.flush();
48+
49+
bos.toByteArray();
50+
}
51+
52+
return true;
53+
} catch (final IOException ex) {
54+
Log.d(TAG, "Exception is not serializable", ex);
55+
return false;
56+
}
57+
}
58+
59+
public static class WorkaroundNotSerializableException extends Exception {
60+
protected WorkaroundNotSerializableException(
61+
final Throwable notSerializableException,
62+
final Throwable cause) {
63+
super(notSerializableException.toString(), cause);
64+
setStackTrace(notSerializableException.getStackTrace());
65+
}
66+
67+
protected WorkaroundNotSerializableException(final Throwable notSerializableException) {
68+
super(notSerializableException.toString());
69+
setStackTrace(notSerializableException.getStackTrace());
70+
}
71+
72+
public static WorkaroundNotSerializableException create(
73+
@NonNull final Exception notSerializableException
74+
) {
75+
// Build a list of the exception + all causes
76+
final List<Throwable> throwableList = new ArrayList<>();
77+
78+
int pos = 0;
79+
Throwable throwableToProcess = notSerializableException;
80+
81+
while (throwableToProcess != null) {
82+
throwableList.add(throwableToProcess);
83+
84+
pos++;
85+
throwableToProcess = throwableToProcess.getCause();
86+
}
87+
88+
// Reverse list so that it starts with the last one
89+
Collections.reverse(throwableList);
90+
91+
// Build exception stack
92+
WorkaroundNotSerializableException cause = null;
93+
for (final Throwable t : throwableList) {
94+
cause = cause == null
95+
? new WorkaroundNotSerializableException(t)
96+
: new WorkaroundNotSerializableException(t, cause);
97+
}
98+
99+
return cause;
100+
}
101+
102+
}
103+
}

app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public class ErrorActivity extends AppCompatActivity {
7777

7878
private ActivityErrorBinding activityErrorBinding;
7979

80+
/**
81+
* Reports a new error by starting a new activity.
82+
* <br/>
83+
* Ensure that the data within errorInfo is serializable otherwise
84+
* an exception will be thrown!<br/>
85+
* {@link EnsureExceptionSerializable} might help.
86+
*
87+
* @param context
88+
* @param errorInfo
89+
*/
8090
public static void reportError(final Context context, final ErrorInfo errorInfo) {
8191
final Intent intent = new Intent(context, ErrorActivity.class);
8292
intent.putExtra(ERROR_INFO, errorInfo);

app/src/main/java/org/schabi/newpipe/player/playererror/PlayerErrorHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.android.exoplayer2.ExoPlaybackException;
1313

1414
import org.schabi.newpipe.R;
15+
import org.schabi.newpipe.error.EnsureExceptionSerializable;
1516
import org.schabi.newpipe.error.ErrorActivity;
1617
import org.schabi.newpipe.error.ErrorInfo;
1718
import org.schabi.newpipe.error.UserAction;
@@ -69,7 +70,7 @@ private void reportError(@NonNull final ExoPlaybackException exception,
6970
ErrorActivity.reportError(
7071
context,
7172
new ErrorInfo(
72-
exception,
73+
EnsureExceptionSerializable.ensureSerializable(exception),
7374
UserAction.PLAY_STREAM,
7475
"Player error[type=" + exception.type + "] occurred while playing: "
7576
+ info.getUrl(),

0 commit comments

Comments
 (0)