/
ErrorHandler.java
420 lines (402 loc) · 14.7 KB
/
ErrorHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.exceptions;
import net.dv8tion.jda.api.requests.ErrorResponse;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* Utility class to simplify error handling with {@link RestAction RestActions} and {@link ErrorResponse ErrorResponses}.
*
* <p><b>Example</b><br>
* <pre>{@code
* // Send message to user and delete it 30 seconds later, handles blocked messages in context channel.
* public void sendMessage(TextChannel context, User user, String content) {
* user.openPrivateChannel()
* .flatMap(channel -> channel.sendMessage(content))
* .delay(Duration.ofSeconds(30))
* .flatMap(Message::delete) // delete after 30 seconds
* .queue(null, new ErrorHandler()
* .ignore(ErrorResponse.UNKNOWN_MESSAGE) // if delete fails that's fine
* .handle(
* ErrorResponse.CANNOT_SEND_TO_USER, // Fallback handling for blocked messages
* (e) -> context.sendMessage("Failed to send message, you block private messages!").queue()));
* }
* }</pre>
*
* @see ErrorResponse
* @see ErrorResponseException
* @see RestAction#queue(Consumer, Consumer)
*
* @since 4.2.0
*/
public class ErrorHandler implements Consumer<Throwable>
{
private static final Consumer<? super Throwable> empty = (e) -> {};
private final Consumer<? super Throwable> base;
private final Map<Predicate<? super Throwable>, Consumer<? super Throwable>> cases = new LinkedHashMap<>();
/**
* Create an ErrorHandler with {@link RestAction#getDefaultFailure()} as base consumer.
* <br>If none of the provided ignore/handle cases apply, the base consumer is applied instead.
*/
public ErrorHandler()
{
this(RestAction.getDefaultFailure());
}
/**
* Create an ErrorHandler with the specified consumer as base consumer.
* <br>If none of the provided ignore/handle cases apply, the base consumer is applied instead.
*
* @param base
* The base {@link Consumer}
*/
public ErrorHandler(@Nonnull Consumer<? super Throwable> base)
{
Checks.notNull(base, "Consumer");
this.base = base;
}
/**
* Ignore the specified set of error responses.
*
* <p><b>Example</b><br>
* <pre>{@code
* // Creates a message with the provided content and deletes it 30 seconds later
* public static void selfDestruct(MessageChannel channel, String content) {
* channel.sendMessage(content)
* .delay(Duration.ofSeconds(30))
* .flatMap(Message::delete)
* .queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
* }
* }</pre>
*
* @param ignored
* Ignored error response
* @param errorResponses
* Additional error responses to ignore
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied ignore cases
*/
@Nonnull
public ErrorHandler ignore(@Nonnull ErrorResponse ignored, @Nonnull ErrorResponse... errorResponses)
{
Checks.notNull(ignored, "ErrorResponse");
Checks.noneNull(errorResponses, "ErrorResponse");
return ignore(EnumSet.of(ignored, errorResponses));
}
/**
* Ignore the specified set of error responses.
*
* <p><b>Example</b><br>
* <pre>{@code
* // Creates a message with the provided content and deletes it 30 seconds later
* public static void selfDestruct(User user, String content) {
* user.openPrivateChannel()
* .flatMap(channel -> channel.sendMessage(content))
* .delay(Duration.ofSeconds(30))
* .flatMap(Message::delete)
* .queue(null, new ErrorHandler().ignore(EnumSet.of(UNKNOWN_MESSAGE, CANNOT_SEND_TO_USER)));
* }
* }</pre>
*
* @param errorResponses
* The error responses to ignore
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied ignore cases
*/
@Nonnull
public ErrorHandler ignore(@Nonnull Collection<ErrorResponse> errorResponses)
{
return handle(errorResponses, empty);
}
/**
* Ignore exceptions of the specified types.
*
* <p><b>Example</b><br>
* <pre>{@code
* // Ignore SocketTimeoutException
* public static void ban(Guild guild, String userId) {
* guild.ban(userId).queue(null, new ErrorHandler().ignore(SocketTimeoutException.class);
* }
* }</pre>
*
* @param clazz
* The class to ignore
* @param classes
* Additional classes to ignore
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied ignore case
*
* @see java.net.SocketTimeoutException
*/
@Nonnull
public ErrorHandler ignore(@Nonnull Class<?> clazz, @Nonnull Class<?>... classes)
{
Checks.notNull(clazz, "Classes");
Checks.noneNull(classes, "Classes");
return ignore(it -> {
if (clazz.isInstance(it))
return true;
for (Class<?> e : classes)
{
if (e.isInstance(it))
return true;
}
return false;
});
}
/**
* Ignore exceptions on specific conditions.
*
* <p><b>Example</b><br>
* <pre>{@code
* // Ignore all exceptions except for ErrorResponseException
* public static void ban(Guild guild, String userId) {
* guild.ban(userId).queue(null, new ErrorHandler().ignore((ex) -> !(ex instanceof ErrorResponseException));
* }
* }</pre>
*
* @param condition
* The condition to check
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied ignore case
*
* @see ErrorResponseException
*/
@Nonnull
public ErrorHandler ignore(@Nonnull Predicate<? super Throwable> condition)
{
return handle(condition, empty);
}
/**
* Handle specific {@link ErrorResponse ErrorResponses}.
* <br>This will apply the specified handler to use instead of the base consumer if one of the provided ErrorResponses happens.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void sendMessage(TextChannel context, User user, String content) {
* user.openPrivateChannel()
* .flatMap(channel -> channel.sendMessage(content))
* .queue(null, new ErrorHandler()
* .handle(ErrorResponse.CANNOT_SEND_TO_USER,
* (ex) -> context.sendMessage("Cannot send direct message, please enable direct messages from server members!").queue()));
* }
* }</pre>
*
* @param response
* The first {@link ErrorResponse} to match
* @param handler
* The alternative handler
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public ErrorHandler handle(@Nonnull ErrorResponse response, @Nonnull Consumer<? super ErrorResponseException> handler)
{
Checks.notNull(response, "ErrorResponse");
return handle(EnumSet.of(response), handler);
}
/**
* Handle specific {@link ErrorResponse ErrorResponses}.
* <br>This will apply the specified handler to use instead of the base consumer if one of the provided ErrorResponses happens.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void sendMessage(TextChannel context, User user, String content) {
* user.openPrivateChannel()
* .flatMap(channel -> channel.sendMessage(content))
* .queue(null, new ErrorHandler()
* .handle(EnumSet.of(ErrorResponse.CANNOT_SEND_TO_USER),
* (ex) -> context.sendMessage("Cannot send direct message, please enable direct messages from server members!").queue()));
* }
* }</pre>
*
* @param errorResponses
* The {@link ErrorResponse ErrorResponses} to match
* @param handler
* The alternative handler
*
* @throws IllegalArgumentException
* If provided with null
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public ErrorHandler handle(@Nonnull Collection<ErrorResponse> errorResponses, @Nonnull Consumer<? super ErrorResponseException> handler)
{
Checks.notNull(handler, "Handler");
Checks.noneNull(errorResponses, "ErrorResponse");
return handle(ErrorResponseException.class, (it) -> errorResponses.contains(it.getErrorResponse()), handler);
}
/**
* Handle specific throwable types.
* <br>This will apply the specified handler if the throwable is of the specified type. The check is done using {@link Class#isInstance(Object)}.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void logErrorResponse(RestAction<?> action) {
* action.queue(null, new ErrorHandler()
* .handle(ErrorResponseException.class,
* (ex) -> System.out.println(ex.getErrorResponse())));
* }
* }</pre>
*
* @param clazz
* The throwable type
* @param handler
* The alternative handler
*
* @param <T>
* The type
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public <T> ErrorHandler handle(@Nonnull Class<T> clazz, @Nonnull Consumer<? super T> handler)
{
Checks.notNull(clazz, "Class");
Checks.notNull(handler, "Handler");
return handle(clazz::isInstance, (ex) -> handler.accept(clazz.cast(ex)));
}
/**
* Handle specific throwable types.
* <br>This will apply the specified handler if the throwable is of the specified type. The check is done using {@link Class#isInstance(Object)}.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void logErrorResponse(RestAction<?> action) {
* action.queue(null, new ErrorHandler()
* .handle(ErrorResponseException.class,
* ErrorResponseException::isServerError,
* (ex) -> System.out.println(ex.getErrorCode() + ": " + ex.getMeaning())));
* }
* }</pre>
*
* @param clazz
* The throwable type
* @param condition
* Additional condition that must apply to use this handler
* @param handler
* The alternative handler
*
* @param <T>
* The type
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public <T> ErrorHandler handle(@Nonnull Class<T> clazz, @Nonnull Predicate<? super T> condition, @Nonnull Consumer<? super T> handler)
{
Checks.notNull(clazz, "Class");
Checks.notNull(handler, "Handler");
return handle(
(it) -> clazz.isInstance(it) && condition.test(clazz.cast(it)),
(ex) -> handler.accept(clazz.cast(ex)));
}
/**
* Handle specific throwable types.
* <br>This will apply the specified handler if the throwable is of the specified type. The check is done using {@link Class#isInstance(Object)}.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void logErrorResponse(RestAction<?> action) {
* action.queue(null, new ErrorHandler()
* .handle(Arrays.asList(Throwable.class),
* (ex) -> ex instanceof Error,
* (ex) -> ex.printStackTrace()));
* }
* }</pre>
*
* @param clazz
* The throwable types
* @param condition
* Additional condition that must apply to use this handler, or null to apply no additional condition
* @param handler
* The alternative handler
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public ErrorHandler handle(@Nonnull Collection<Class<?>> clazz, @Nullable Predicate<? super Throwable> condition, @Nonnull Consumer<? super Throwable> handler)
{
Checks.noneNull(clazz, "Class");
Checks.notNull(handler, "Handler");
List<Class<?>> classes = new ArrayList<>(clazz);
Predicate<? super Throwable> check = (it) -> classes.stream().anyMatch(c -> c.isInstance(it)) && (condition == null || condition.test(it));
return handle(check, handler);
}
/**
* Handle specific conditions.
*
* <p><b>Example</b><br>
* <pre>{@code
* public static void logErrorResponse(RestAction<?> action) {
* action.queue(null, new ErrorHandler()
* .handle(
* (ex) -> !(ex instanceof ErrorResponseException),
* Throwable::printStackTrace));
* }
* }</pre>
*
* @param condition
* Condition that must apply to use this handler
* @param handler
* The alternative handler
*
* @return This ErrorHandler with the applied handler
*/
@Nonnull
public ErrorHandler handle(@Nonnull Predicate<? super Throwable> condition, @Nonnull Consumer<? super Throwable> handler)
{
Checks.notNull(condition, "Condition");
Checks.notNull(handler, "Handler");
cases.put(condition, handler);
return this;
}
@Override
public void accept(Throwable t)
{
for (Map.Entry<Predicate<? super Throwable>, Consumer<? super Throwable>> entry : cases.entrySet())
{
Predicate<? super Throwable> condition = entry.getKey();
Consumer<? super Throwable> callback = entry.getValue();
if (condition.test(t))
{
callback.accept(t);
return;
}
}
base.accept(t);
}
}