Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added common Exceptions.throwIfAny to throw a collection of exceptions #2601

Merged
merged 4 commits into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/main/java/rx/exceptions/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package rx.exceptions;

import java.util.HashSet;
import java.util.Set;
import java.util.*;

/**
* @warn javadoc class description missing
Expand Down Expand Up @@ -148,5 +147,32 @@ public static Throwable getFinalCause(Throwable e) {
}
return e;
}

/**
* Throws a single or multiple exceptions contained in the collection, wrapping it into
* {@code CompositeException} if necessary.
* @param exceptions the collection of exceptions. If null or empty, no exception is thrown.
* If the collection contains a single exception, that exception is either thrown as-is or wrapped into a
* CompositeException. Multiple exceptions are wrapped into a CompositeException.
* @param whileText the circumstance string to be appended to the thrown CompositeException, inserted after
* the sentences "Exception" and "Multiple exceptions".
*/
public static void throwIfAny(Collection<? extends Throwable> exceptions, String whileText) {
if (exceptions != null && !exceptions.isEmpty()) {
if (exceptions.size() == 1) {
Throwable t = exceptions.iterator().next();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else
if (t instanceof Error) {
throw (Error) t;
} else {
throw new CompositeException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right here we have a single exception so why do we need to wrap it in CompositeException? The Exceptions.propagate method could be called here like it was doing in publish.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more clearly ... why doesn't this entire if-block just delegate to propagate since it is replicating that logic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it threw CompositeExceptions so far and I couldn't be sure if the message was relevant or not for the potential recipient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In OperatorPublish it didn't, it used propagate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the subjects used propagate as well.

"Exception" + whileText, exceptions);
}
} else {
throw new CompositeException(
"Multiple exceptions" + whileText, exceptions);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be easier to read:

if (exceptions.size() == 1) {
    Throwable t = exceptions.iterator().next();
    if (t instanceof RuntimeException) {
        throw (RuntimeException) t
    }
    if (t instanceof Error) {
        throw (Error) t;
    }
}
throw new CompositeException("Exception" + whileText, exceptions);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the original places distinguished between single and multiple expections in the message of CompositeException.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the discussion at #2602 ... this is an example where @akarnokd and I have slightly different styles, but it's okay. I always have else if on the same like, here it is split, but I can still read the code and it works so I choose to not make a big deal out of it.

Sometimes when @akarnokd and I end up modifying each others code we'll see some lines get modified to personal style if we touch them, but we don't go around explicitly trying to change code just to match a personal preference ... if for no other reason than to not clutter changelogs and diffs.

}
}
}
28 changes: 6 additions & 22 deletions src/main/java/rx/internal/operators/OperatorPublish.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@
*/
package rx.internal.operators;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.*;
import java.util.concurrent.atomic.*;

import rx.*;
import rx.Observable;
import rx.Producer;
import rx.Subscriber;
import rx.Subscription;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.exceptions.MissingBackpressureException;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.exceptions.*;
import rx.functions.*;
import rx.internal.util.RxRingBuffer;
import rx.observables.ConnectableObservable;
import rx.subscriptions.Subscriptions;
Expand Down Expand Up @@ -173,13 +163,7 @@ public void onError(Throwable e) {
errors.add(e2);
}
}
if (errors != null) {
if (errors.size() == 1) {
Exceptions.propagate(errors.get(0));
} else {
throw new CompositeException("Errors while emitting onError", errors);
}
}
Exceptions.throwIfAny(errors, " while emitting onError");
}

@Override
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/rx/internal/util/SubscriptionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

import rx.Subscription;
import rx.exceptions.CompositeException;
import rx.exceptions.*;

/**
* Subscription that represents a group of Subscriptions that are unsubscribed together.
Expand Down Expand Up @@ -106,20 +106,7 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
es.add(e);
}
}
if (es != null) {
if (es.size() == 1) {
Throwable t = es.get(0);
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new CompositeException(
"Failed to unsubscribe to 1 or more subscriptions.", es);
}
} else {
throw new CompositeException(
"Failed to unsubscribe to 2 or more subscriptions.", es);
}
}
Exceptions.throwIfAny(es, " while unsubscribing.");
}
/* perf support */
public void clear() {
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/rx/internal/util/SubscriptionRandomList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Set;

import rx.Subscription;
import rx.exceptions.CompositeException;
import rx.exceptions.*;
import rx.functions.Action1;

/**
Expand Down Expand Up @@ -155,19 +155,6 @@ private static <T extends Subscription> void unsubscribeFromAll(Collection<T> su
es.add(e);
}
}
if (es != null) {
if (es.size() == 1) {
Throwable t = es.get(0);
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new CompositeException(
"Failed to unsubscribe to 1 or more subscriptions.", es);
}
} else {
throw new CompositeException(
"Failed to unsubscribe to 2 or more subscriptions.", es);
}
}
Exceptions.throwIfAny(es, " while unsubscribing.");
}
}
12 changes: 2 additions & 10 deletions src/main/java/rx/subjects/AsyncSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
package rx.subjects;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

import rx.Observer;
import rx.annotations.Experimental;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
Expand Down Expand Up @@ -122,13 +120,7 @@ public void onError(final Throwable e) {
}
}

if (errors != null) {
if (errors.size() == 1) {
Exceptions.propagate(errors.get(0));
} else {
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
}
}
Exceptions.throwIfAny(errors, " while emitting AsyncSubject.onError");
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/rx/subjects/BehaviorSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
package rx.subjects;


import java.util.ArrayList;
import java.util.List;
import java.util.*;

import rx.Observer;
import rx.annotations.Experimental;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
Expand Down Expand Up @@ -148,13 +146,7 @@ public void onError(Throwable e) {
}
}

if (errors != null) {
if (errors.size() == 1) {
Exceptions.propagate(errors.get(0));
} else {
throw new CompositeException("Errors while emitting AsyncSubject.onError", errors);
}
}
Exceptions.throwIfAny(errors, " while emitting BehaviorSubject.onError");
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/rx/subjects/PublishSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/
package rx.subjects;

import java.util.ArrayList;
import java.util.List;
import java.util.*;

import rx.Observer;
import rx.annotations.Experimental;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
Expand Down Expand Up @@ -106,13 +104,7 @@ public void onError(final Throwable e) {
errors.add(e2);
}
}
if (errors != null) {
if (errors.size() == 1) {
Exceptions.propagate(errors.get(0));
} else {
throw new CompositeException("Errors while emitting PublishSubject.onError", errors);
}
}
Exceptions.throwIfAny(errors, " while emitting PublishSubject.onError");
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/main/java/rx/subjects/ReplaySubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@
package rx.subjects;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import rx.*;
import rx.Observer;
import rx.Scheduler;
import rx.annotations.Experimental;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.*;
import rx.internal.operators.NotificationLite;
import rx.internal.util.UtilityFunctions;
import rx.schedulers.Timestamped;
Expand Down Expand Up @@ -394,13 +391,7 @@ public void onError(final Throwable e) {
}
}

if (errors != null) {
if (errors.size() == 1) {
Exceptions.propagate(errors.get(0));
} else {
throw new CompositeException("Errors while emitting ReplaySubject.onError", errors);
}
}
Exceptions.throwIfAny(errors, " while emitting ReplaySubject.onError");
}
}

Expand Down
17 changes: 2 additions & 15 deletions src/main/java/rx/subscriptions/CompositeSubscription.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Set;

import rx.Subscription;
import rx.exceptions.CompositeException;
import rx.exceptions.*;

/**
* Subscription that represents a group of Subscriptions that are unsubscribed together.
Expand Down Expand Up @@ -141,19 +141,6 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
es.add(e);
}
}
if (es != null) {
if (es.size() == 1) {
Throwable t = es.get(0);
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new CompositeException(
"Failed to unsubscribe to 1 or more subscriptions.", es);
}
} else {
throw new CompositeException(
"Failed to unsubscribe to 2 or more subscriptions.", es);
}
}
Exceptions.throwIfAny(es, " while unsubscribing.");
}
}