-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,7 @@ | |
*/ | ||
package rx.exceptions; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.*; | ||
|
||
/** | ||
* @warn javadoc class description missing | ||
|
@@ -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( | ||
"Exception" + whileText, exceptions); | ||
} | ||
} else { | ||
throw new CompositeException( | ||
"Multiple exceptions" + whileText, exceptions); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be easier to read:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
? TheExceptions.propagate
method could be called here like it was doing inpublish
.There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 usedpropagate
.There was a problem hiding this comment.
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.