RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

throwing a Serializable RuntimeException in OnNext requires OnNext value to be Serializable as well

Open saharh opened this issue 10 years ago • 5 comments

when throwing a runtime exception during OnNext, the exception is wrapped within OnErrorThrowable$OnNextValue, which features access to the value emitted in OnNext. this feature comes at a cost - If one would wish to serialize/deserialize the exception or any object containing that exception, one would also be required to have the type of item emitted by OnNext also implement Serializable (which could lead to whole hierarchy of objects be serializable). While the original runtime exception thrown is serializable, the OnErrorThrowable$OnNextValue which includes reference to the onnext emitted item, isn't. This leads to java.io.NotSerializableException thrown when trying to serialize the exception:

Caused by: java.io.NotSerializableException: com.example.SomeUnserializableObject
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
        at java.lang.Throwable.writeObject(Throwable.java:436)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.io.ObjectO

I dont see any way around it other than avoiding throwing of RuntimeException during onNext, and instead move to flatmap and return Observable.error(exception).

saharh avatar Sep 15 '16 19:09 saharh

We can mark the value as transient in the exception.

On Thu, Sep 15, 2016 at 3:29 PM saharh [email protected] wrote:

when throwing a runtime exception during OnNext, the exception is wrapped within OnErrorThrowable$OnNextValue, which features access to the value emitted in OnNext. this feature comes at a cost - If one would wish to serialize/deserialize the exception or any object containing that exception, one would also be required to have the type of item emitted by OnNext also implement Serializable (which could lead to whole hierarchy of objects be serializable). While the original runtime exception thrown is serializable, the OnErrorThrowable$OnNextValue which includes reference to the onnext emitted item, isn't. This leads to java.io.NotSerializableException thrown when trying to serialize the exception:

                                                                     Caused by: java.io.NotSerializableException: com.example.sample.SomeUnserializableObject
                                                                        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
                                                                        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
                                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
                                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
                                                                        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
                                                                        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
                                                                        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
                                                                        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
                                                                        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
                                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
                                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
                                                                        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
                                                                        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
                                                                        at java.lang.Throwable.writeObject(Throwable.java:436)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.io.ObjectO

I dont see any way around it other than avoiding throwing of RuntimeException during onNext, and instead move to flatmap and return Observable.error(exception).

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxJava/issues/4562, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEd3gv1EWHy9yKNEmMMtIbHjEAqpjks5qqZyhgaJpZM4J-QSY .

JakeWharton avatar Sep 15 '16 19:09 JakeWharton

I must ask: what's your use case where you want to serialize exceptions? Are passing them over to some other process maybe?

rzsombor avatar Sep 18 '16 15:09 rzsombor

It is a rather rare use case, but i do pass a result which might include an exception between activities in android, which requires the object to be serializable

saharh avatar Sep 19 '16 14:09 saharh

See #4841

akarnokd avatar Nov 12 '16 12:11 akarnokd

Closing via #4841

akarnokd avatar Nov 12 '16 18:11 akarnokd