Skip to content

Commit 5c5452a

Browse files
Add try/catch abstraction for RestAction operators (discord-jda#2044)
1 parent 5d4eeb4 commit 5c5452a

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/main/java/net/dv8tion/jda/internal/requests/restaction/operator/DelayRestAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public DelayRestAction(RestAction<T> action, TimeUnit unit, long delay, Schedule
4343
@Override
4444
public void queue(@Nullable Consumer<? super T> success, @Nullable Consumer<? super Throwable> failure)
4545
{
46-
action.queue((result) ->
46+
handle(action, failure, (result) ->
4747
scheduler.schedule(() ->
4848
doSuccess(success, result),
49-
delay, unit),
50-
contextWrap(failure));
49+
delay, unit)
50+
);
5151
}
5252

5353
@Override

src/main/java/net/dv8tion/jda/internal/requests/restaction/operator/FlatMapRestAction.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,15 @@ private RestAction<O> supply(I input)
4747
@Override
4848
public void queue(@Nullable Consumer<? super O> success, @Nullable Consumer<? super Throwable> failure)
4949
{
50-
Consumer<? super Throwable> contextFailure = contextWrap(failure);
51-
action.queue((result) -> {
50+
Consumer<? super Throwable> catcher = contextWrap(failure);
51+
handle(action, catcher, (result) -> {
5252
if (condition != null && !condition.test(result))
5353
return;
5454
RestAction<O> then = supply(result);
55-
if (then == null)
56-
doFailure(contextFailure, new IllegalStateException("FlatMap operand is null"));
57-
else
58-
then.queue(success, contextFailure);
59-
}, contextFailure);
55+
if (then == null) // caught by handle try/catch abstraction
56+
throw new IllegalStateException("FlatMap operand is null");
57+
then.queue(success, catcher);
58+
});
6059
}
6160

6261
@Override

src/main/java/net/dv8tion/jda/internal/requests/restaction/operator/MapRestAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public MapRestAction(RestAction<I> action, Function<? super I, ? extends O> func
3838
@Override
3939
public void queue(@Nullable Consumer<? super O> success, @Nullable Consumer<? super Throwable> failure)
4040
{
41-
action.queue((result) -> doSuccess(success, function.apply(result)), contextWrap(failure));
41+
handle(action, failure,
42+
(result) -> doSuccess(success, function.apply(result))
43+
);
4244
}
4345

4446
@Override

src/main/java/net/dv8tion/jda/internal/requests/restaction/operator/RestActionOperator.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ protected static void doFailure(Consumer<? super Throwable> callback, Throwable
5454
throw (Error) throwable;
5555
}
5656

57+
protected void handle(RestAction<I> action, Consumer<? super Throwable> failure, Consumer<? super I> success)
58+
{
59+
Consumer<? super Throwable> catcher = contextWrap(failure);
60+
action.queue((result) -> {
61+
try
62+
{
63+
if (success != null)
64+
success.accept(result);
65+
}
66+
catch (Throwable ex)
67+
{
68+
doFailure(catcher, ex);
69+
}
70+
}, catcher);
71+
}
72+
5773
@Nonnull
5874
@Override
5975
public JDA getJDA()
@@ -86,6 +102,7 @@ public RestAction<O> deadline(long timestamp)
86102
return this;
87103
}
88104

105+
@Nullable
89106
protected <T> RestAction<T> applyContext(RestAction<T> action)
90107
{
91108
if (action == null)
@@ -97,6 +114,7 @@ protected <T> RestAction<T> applyContext(RestAction<T> action)
97114
return action;
98115
}
99116

117+
@Nullable
100118
protected Consumer<? super Throwable> contextWrap(@Nullable Consumer<? super Throwable> callback)
101119
{
102120
if (callback instanceof ContextException.ContextConsumer)

0 commit comments

Comments
 (0)