Future will fail when the handler which is passed to Future.future throws an Exception. (#3057)

* Future will fail on `Future.future` throws an Exception.

Signed-off-by: iseki <admin@iseki.space>

* Modify testcase and fix format.

Signed-off-by: iseki <admin@iseki.space>
This commit is contained in:
cpdyj
2019-08-05 22:45:46 +08:00
committed by Julien Viet
parent 72f81f5b52
commit d14f832823
2 changed files with 9 additions and 9 deletions

View File

@@ -36,7 +36,11 @@ public interface Future<T> extends AsyncResult<T> {
*/
static <T> Future<T> future(Handler<Promise<T>> handler) {
Promise<T> promise = Promise.promise();
handler.handle(promise);
try {
handler.handle(promise);
} catch (Exception e){
promise.tryFail(e);
}
return promise.future();
}

View File

@@ -65,14 +65,10 @@ public class FutureTest extends VertxTestBase {
assertSame(f2, ref.get().future());
assertEquals(1, count.get());
new Checker<>(f2).assertFailed(cause);
try {
Future.future(f -> {
throw cause;
});
fail();
} catch (Exception e) {
assertSame(cause, e);
}
Future f3 = Future.future(f -> {
throw cause;
});
assertSame(cause, f3.cause());
}
@Test