Skip to content

Commit

Permalink
Make ActionListener.wrap(Runnable) more Predictable (#70375)
Browse files Browse the repository at this point in the history
The current implementation behaved in a non-obvious manner.
If the runnable throws but is executed in `onResponse` then it will
actually be executed twice and the exception be lost.
This means that the exception during the first call will simply be quietly swallowed.
in case the runnable (as is often the case) is a `RunOnce` or similar, where the second
execution in `onFailure` is a noop this means any exception is completely swallowed.
  • Loading branch information
original-brownbear authored Mar 15, 2021
1 parent d312e8c commit 318ae89
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,33 @@ public String toString() {
* @return a listener that listens for responses and invokes the runnable when received
*/
static <Response> ActionListener<Response> wrap(Runnable runnable) {
return wrap(r -> runnable.run(), e -> runnable.run());
return new ActionListener<>() {
@Override
public void onResponse(Response response) {
try {
runnable.run();
} catch (RuntimeException e) {
assert false : e;
throw e;
}
}

@Override
public void onFailure(Exception e) {
try {
runnable.run();
} catch (RuntimeException ex) {
ex.addSuppressed(e);
assert false : ex;
throw ex;
}
}

@Override
public String toString() {
return "RunnableWrappingActionListener{" + runnable + "}";
}
};
}

/**
Expand Down

0 comments on commit 318ae89

Please sign in to comment.