Skip to content

Commit f84cdce

Browse files
Merge pull request #2601 from akarnokd/CommonUnsubscribeThrow
Added common Exceptions.throwIfAny to throw a collection of exceptions
2 parents 5829e15 + 0d4b610 commit f84cdce

File tree

9 files changed

+54
-113
lines changed

9 files changed

+54
-113
lines changed

src/main/java/rx/exceptions/Exceptions.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package rx.exceptions;
1717

1818
import java.util.HashSet;
19+
import java.util.List;
1920
import java.util.Set;
2021

22+
import rx.annotations.Experimental;
23+
2124
/**
2225
* @warn javadoc class description missing
2326
*/
@@ -27,7 +30,9 @@ private Exceptions() {
2730
}
2831

2932
/**
30-
* @warn javadoc missing
33+
* Convenience method to throw a {@code RuntimeException} and {@code Error} directly
34+
* or wrap any other exception type into a {@code RuntimeException}.
35+
* @param t the exception to throw directly or wrapped
3136
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return
3237
* value; {@code propagate} does not actually return anything
3338
*/
@@ -48,7 +53,6 @@ public static RuntimeException propagate(Throwable t) {
4853
throw new RuntimeException(t);
4954
}
5055
}
51-
5256
/**
5357
* Throws a particular {@code Throwable} only if it belongs to a set of "fatal" error varieties. These
5458
* varieties are as follows:
@@ -148,5 +152,30 @@ public static Throwable getFinalCause(Throwable e) {
148152
}
149153
return e;
150154
}
151-
155+
/**
156+
* Throws a single or multiple exceptions contained in the collection, wrapping it into
157+
* {@code CompositeException} if necessary.
158+
* @param exceptions the collection of exceptions. If null or empty, no exception is thrown.
159+
* If the collection contains a single exception, that exception is either thrown as-is or wrapped into a
160+
* CompositeException. Multiple exceptions are wrapped into a CompositeException.
161+
*/
162+
@Experimental
163+
public static void throwIfAny(List<? extends Throwable> exceptions) {
164+
if (exceptions != null && !exceptions.isEmpty()) {
165+
if (exceptions.size() == 1) {
166+
Throwable t = exceptions.get(0);
167+
// had to manually inline propagate because some tests attempt StackOverflowError
168+
// and can't handle it with the stack space remaining
169+
if (t instanceof RuntimeException) {
170+
throw (RuntimeException) t;
171+
} else if (t instanceof Error) {
172+
throw (Error) t;
173+
} else {
174+
throw new RuntimeException(t);
175+
}
176+
}
177+
throw new CompositeException(
178+
"Multiple exceptions", exceptions);
179+
}
180+
}
152181
}

src/main/java/rx/internal/operators/OperatorPublish.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,13 @@
1515
*/
1616
package rx.internal.operators;
1717

18-
import java.util.ArrayList;
19-
import java.util.LinkedHashMap;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.concurrent.atomic.AtomicLong;
23-
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
18+
import java.util.*;
19+
import java.util.concurrent.atomic.*;
2420

21+
import rx.*;
2522
import rx.Observable;
26-
import rx.Producer;
27-
import rx.Subscriber;
28-
import rx.Subscription;
29-
import rx.exceptions.CompositeException;
30-
import rx.exceptions.Exceptions;
31-
import rx.exceptions.MissingBackpressureException;
32-
import rx.functions.Action0;
33-
import rx.functions.Action1;
34-
import rx.functions.Func1;
23+
import rx.exceptions.*;
24+
import rx.functions.*;
3525
import rx.internal.util.RxRingBuffer;
3626
import rx.observables.ConnectableObservable;
3727
import rx.subscriptions.Subscriptions;
@@ -173,13 +163,7 @@ public void onError(Throwable e) {
173163
errors.add(e2);
174164
}
175165
}
176-
if (errors != null) {
177-
if (errors.size() == 1) {
178-
Exceptions.propagate(errors.get(0));
179-
} else {
180-
throw new CompositeException("Errors while emitting onError", errors);
181-
}
182-
}
166+
Exceptions.throwIfAny(errors);
183167
}
184168

185169
@Override

src/main/java/rx/internal/util/SubscriptionList.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
import rx.Subscription;
25-
import rx.exceptions.CompositeException;
25+
import rx.exceptions.*;
2626

2727
/**
2828
* Subscription that represents a group of Subscriptions that are unsubscribed together.
@@ -106,20 +106,7 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
106106
es.add(e);
107107
}
108108
}
109-
if (es != null) {
110-
if (es.size() == 1) {
111-
Throwable t = es.get(0);
112-
if (t instanceof RuntimeException) {
113-
throw (RuntimeException) t;
114-
} else {
115-
throw new CompositeException(
116-
"Failed to unsubscribe to 1 or more subscriptions.", es);
117-
}
118-
} else {
119-
throw new CompositeException(
120-
"Failed to unsubscribe to 2 or more subscriptions.", es);
121-
}
122-
}
109+
Exceptions.throwIfAny(es);
123110
}
124111
/* perf support */
125112
public void clear() {

src/main/java/rx/internal/util/SubscriptionRandomList.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
import rx.Subscription;
26-
import rx.exceptions.CompositeException;
26+
import rx.exceptions.*;
2727
import rx.functions.Action1;
2828

2929
/**
@@ -155,19 +155,6 @@ private static <T extends Subscription> void unsubscribeFromAll(Collection<T> su
155155
es.add(e);
156156
}
157157
}
158-
if (es != null) {
159-
if (es.size() == 1) {
160-
Throwable t = es.get(0);
161-
if (t instanceof RuntimeException) {
162-
throw (RuntimeException) t;
163-
} else {
164-
throw new CompositeException(
165-
"Failed to unsubscribe to 1 or more subscriptions.", es);
166-
}
167-
} else {
168-
throw new CompositeException(
169-
"Failed to unsubscribe to 2 or more subscriptions.", es);
170-
}
171-
}
158+
Exceptions.throwIfAny(es);
172159
}
173160
}

src/main/java/rx/subjects/AsyncSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package rx.subjects;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
18+
import java.util.*;
2019

2120
import rx.Observer;
2221
import rx.annotations.Experimental;
23-
import rx.exceptions.CompositeException;
2422
import rx.exceptions.Exceptions;
2523
import rx.functions.Action1;
2624
import rx.internal.operators.NotificationLite;
@@ -122,13 +120,7 @@ public void onError(final Throwable e) {
122120
}
123121
}
124122

125-
if (errors != null) {
126-
if (errors.size() == 1) {
127-
Exceptions.propagate(errors.get(0));
128-
} else {
129-
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
130-
}
131-
}
123+
Exceptions.throwIfAny(errors);
132124
}
133125
}
134126

src/main/java/rx/subjects/BehaviorSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
package rx.subjects;
1717

1818

19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.*;
2120

2221
import rx.Observer;
2322
import rx.annotations.Experimental;
24-
import rx.exceptions.CompositeException;
2523
import rx.exceptions.Exceptions;
2624
import rx.functions.Action1;
2725
import rx.internal.operators.NotificationLite;
@@ -148,13 +146,7 @@ public void onError(Throwable e) {
148146
}
149147
}
150148

151-
if (errors != null) {
152-
if (errors.size() == 1) {
153-
Exceptions.propagate(errors.get(0));
154-
} else {
155-
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
156-
}
157-
}
149+
Exceptions.throwIfAny(errors);
158150
}
159151
}
160152

src/main/java/rx/subjects/PublishSubject.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616
package rx.subjects;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
18+
import java.util.*;
2019

2120
import rx.Observer;
2221
import rx.annotations.Experimental;
23-
import rx.exceptions.CompositeException;
2422
import rx.exceptions.Exceptions;
2523
import rx.functions.Action1;
2624
import rx.internal.operators.NotificationLite;
@@ -106,13 +104,7 @@ public void onError(final Throwable e) {
106104
errors.add(e2);
107105
}
108106
}
109-
if (errors != null) {
110-
if (errors.size() == 1) {
111-
Exceptions.propagate(errors.get(0));
112-
} else {
113-
throw new CompositeException("Errors while emitting PublishSubject.onError", errors);
114-
}
115-
}
107+
Exceptions.throwIfAny(errors);
116108
}
117109
}
118110

src/main/java/rx/subjects/ReplaySubject.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
package rx.subjects;
1717

1818
import java.lang.reflect.Array;
19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.*;
2120
import java.util.concurrent.TimeUnit;
2221
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
2322

23+
import rx.*;
2424
import rx.Observer;
25-
import rx.Scheduler;
2625
import rx.annotations.Experimental;
27-
import rx.exceptions.CompositeException;
2826
import rx.exceptions.Exceptions;
29-
import rx.functions.Action1;
30-
import rx.functions.Func1;
27+
import rx.functions.*;
3128
import rx.internal.operators.NotificationLite;
3229
import rx.internal.util.UtilityFunctions;
3330
import rx.schedulers.Timestamped;
@@ -394,13 +391,7 @@ public void onError(final Throwable e) {
394391
}
395392
}
396393

397-
if (errors != null) {
398-
if (errors.size() == 1) {
399-
Exceptions.propagate(errors.get(0));
400-
} else {
401-
throw new CompositeException("Errors while emitting ReplaySubject.onError", errors);
402-
}
403-
}
394+
Exceptions.throwIfAny(errors);
404395
}
405396
}
406397

src/main/java/rx/subscriptions/CompositeSubscription.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
import rx.Subscription;
26-
import rx.exceptions.CompositeException;
26+
import rx.exceptions.*;
2727

2828
/**
2929
* Subscription that represents a group of Subscriptions that are unsubscribed together.
@@ -141,19 +141,6 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
141141
es.add(e);
142142
}
143143
}
144-
if (es != null) {
145-
if (es.size() == 1) {
146-
Throwable t = es.get(0);
147-
if (t instanceof RuntimeException) {
148-
throw (RuntimeException) t;
149-
} else {
150-
throw new CompositeException(
151-
"Failed to unsubscribe to 1 or more subscriptions.", es);
152-
}
153-
} else {
154-
throw new CompositeException(
155-
"Failed to unsubscribe to 2 or more subscriptions.", es);
156-
}
157-
}
144+
Exceptions.throwIfAny(es);
158145
}
159146
}

0 commit comments

Comments
 (0)