/
ErrorResponseException.java
449 lines (418 loc) · 15.4 KB
/
ErrorResponseException.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* 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.Response;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.JDALogger;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Indicates an unhandled error that is returned by Discord API Request using {@link net.dv8tion.jda.api.requests.RestAction RestAction}
* <br>It holds an {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponse}
*
* @see net.dv8tion.jda.api.exceptions.ErrorHandler
*/
public class ErrorResponseException extends RuntimeException
{
private final ErrorResponse errorResponse;
private final Response response;
private final String meaning;
private final int code;
private final List<SchemaError> schemaErrors;
/**
* Creates a new ErrorResponseException instance
*
* @param errorResponse
* The {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponse} corresponding
* for the received error response from Discord
* @param response
* The Discord Response causing the ErrorResponse
*/
private ErrorResponseException(ErrorResponse errorResponse, Response response, int code, String meaning, List<SchemaError> schemaErrors)
{
super(code + ": " + meaning + (schemaErrors.isEmpty() ? ""
: "\n" + schemaErrors.stream().map(SchemaError::toString).collect(Collectors.joining("\n"))));
this.response = response;
if (response != null && response.getException() != null)
initCause(response.getException());
this.errorResponse = errorResponse;
this.code = code;
this.meaning = meaning;
this.schemaErrors = schemaErrors;
}
/**
* Whether this is an internal server error from discord (status 500)
*
* @return True, if this is an internal server error
* {@link net.dv8tion.jda.api.requests.ErrorResponse#SERVER_ERROR ErrorResponse.SERVER_ERROR}
*/
public boolean isServerError()
{
return errorResponse == ErrorResponse.SERVER_ERROR;
}
/**
* The meaning for this error.
* <br>It is possible that the value from this method is different for {@link #isServerError() server errors}
*
* @return Never-null meaning of this error.
*/
public String getMeaning()
{
return meaning;
}
/**
* The discord error code for this error response.
*
* @return The discord error code.
*
* @see <a href="https://discord.com/developers/docs/topics/opcodes-and-status-codes#json-json-error-codes" target="_blank">Discord Error Codes</a>
*/
public int getErrorCode()
{
return code;
}
/**
* The {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponse} corresponding
* for the received error response from Discord
*
* @return {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponse}
*/
public ErrorResponse getErrorResponse()
{
return errorResponse;
}
/**
* The Discord Response causing the ErrorResponse
*
* @return {@link net.dv8tion.jda.api.requests.Response Response}
*/
public Response getResponse()
{
return response;
}
/**
* The {@link SchemaError SchemaErrors} for this error response.
* <br>These errors provide more context of what part in the body caused the error, and more explanation for the error itself.
*
* @return Possibly-empty list of {@link SchemaError SchemaError}
*/
@Nonnull
public List<SchemaError> getSchemaErrors()
{
return schemaErrors;
}
public static ErrorResponseException create(ErrorResponse errorResponse, Response response)
{
String meaning = errorResponse.getMeaning();
int code = errorResponse.getCode();
List<SchemaError> schemaErrors = new ArrayList<>();
try
{
Optional<DataObject> optObj = response.optObject();
if (response.isError() && response.getException() != null)
{
// this generally means that an exception occurred trying to
//make an http request. e.g.:
//SocketTimeoutException/ UnknownHostException
code = response.code;
meaning = response.getException().getClass().getName();
}
else if (optObj.isPresent())
{
DataObject obj = optObj.get();
if (!obj.isNull("code") || !obj.isNull("message"))
{
if (!obj.isNull("code"))
code = obj.getInt("code");
if (!obj.isNull("message"))
meaning = obj.getString("message");
}
else
{
// This means that neither code or message is provided
//In that case we simply put the raw response in place!
code = response.code;
meaning = obj.toString();
}
obj.optObject("errors").ifPresent(schema -> parseSchema(schemaErrors, "", schema));
}
else
{
// error response body is not JSON
code = response.code;
meaning = response.getString();
}
}
catch (Exception e)
{
JDALogger.getLog(ErrorResponseException.class).error("Failed to parse parts of error response. Body: {}", response.getString(), e);
}
return new ErrorResponseException(errorResponse, response, code, meaning, schemaErrors);
}
private static void parseSchema(List<SchemaError> schemaErrors, String currentLocation, DataObject errors)
{
// check what kind of errors we are dealing with
for (String name : errors.keys())
{
if (name.equals("_errors"))
{
schemaErrors.add(parseSchemaError(currentLocation, errors));
continue;
}
DataObject schemaError = errors.getObject(name);
if (!schemaError.isNull("_errors"))
{
// We are dealing with an Object Error
schemaErrors.add(parseSchemaError(currentLocation + name, schemaError));
}
else if (schemaError.keys().stream().allMatch(Helpers::isNumeric))
{
// We have an Array Error
for (String index : schemaError.keys())
{
DataObject properties = schemaError.getObject(index);
String location = String.format("%s%s[%s].", currentLocation, name, index);
if (properties.hasKey("_errors"))
schemaErrors.add(parseSchemaError(location.substring(0, location.length()-1), properties));
else
parseSchema(schemaErrors, location, properties);
}
}
else
{
// We have a nested schema error, use recursion!
String location = String.format("%s%s.", currentLocation, name);
parseSchema(schemaErrors, location, schemaError);
}
}
}
private static SchemaError parseSchemaError(String location, DataObject obj)
{
List<ErrorCode> codes = obj.getArray("_errors")
.stream(DataArray::getObject)
.map(json -> new ErrorCode(json.getString("code"), json.getString("message")))
.collect(Collectors.toList());
return new SchemaError(location, codes);
}
/**
* Ignore the specified set of error responses.
*
* <h2>Example</h2>
* <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).queue((message) ->
* message.delete().queueAfter(30, SECONDS, null, ignore(EnumSet.of(UNKNOWN_MESSAGE)))
* );
* }
* }</pre>
*
* @param set
* Set of ignored error responses
*
* @throws IllegalArgumentException
* If provided with null or an empty collection
*
* @return {@link Consumer} decorator for {@link RestAction#getDefaultFailure()}
* which ignores the specified {@link ErrorResponse ErrorResponses}
*/
@Nonnull
public static Consumer<Throwable> ignore(@Nonnull Collection<ErrorResponse> set)
{
return ignore(RestAction.getDefaultFailure(), set);
}
/**
* Ignore the specified set of error responses.
*
* <h2>Example</h2>
* <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).queue((message) ->
* message.delete().queueAfter(30, SECONDS, null, ignore(UNKNOWN_MESSAGE))
* );
* }
* }</pre>
*
* @param ignored
* Ignored error response
* @param errorResponses
* Additional error responses to ignore
*
* @throws IllegalArgumentException
* If provided with null
*
* @return {@link Consumer} decorator for {@link RestAction#getDefaultFailure()}
* which ignores the specified {@link ErrorResponse ErrorResponses}
*/
@Nonnull
public static Consumer<Throwable> ignore(@Nonnull ErrorResponse ignored, @Nonnull ErrorResponse... errorResponses)
{
return ignore(RestAction.getDefaultFailure(), ignored, errorResponses);
}
/**
* Ignore the specified set of error responses.
*
* <h2>Example</h2>
* <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).queue((message) ->
* message.delete().queueAfter(30, SECONDS, null, ignore(Throwable::printStackTrace, UNKNOWN_MESSAGE))
* );
* }
* }</pre>
*
* @param orElse
* Behavior to default to if the error response is not ignored
* @param ignored
* Ignored error response
* @param errorResponses
* Additional error responses to ignore
*
* @throws IllegalArgumentException
* If provided with null
*
* @return {@link Consumer} decorator for the provided callback
* which ignores the specified {@link ErrorResponse ErrorResponses}
*/
@Nonnull
public static Consumer<Throwable> ignore(@Nonnull Consumer<? super Throwable> orElse, @Nonnull ErrorResponse ignored, @Nonnull ErrorResponse... errorResponses)
{
return ignore(orElse, EnumSet.of(ignored, errorResponses));
}
/**
* Ignore the specified set of error responses.
*
* <h2>Example</h2>
* <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).queue((message) ->
* message.delete().queueAfter(30, SECONDS, null, ignore(Throwable::printStackTrace, EnumSet.of(UNKNOWN_MESSAGE)))
* );
* }
* }</pre>
*
* @param orElse
* Behavior to default to if the error response is not ignored
* @param set
* Set of ignored error responses
*
* @throws IllegalArgumentException
* If provided with null or an empty collection
*
* @return {@link Consumer} decorator for the provided callback
* which ignores the specified {@link ErrorResponse ErrorResponses}
*/
@Nonnull
public static Consumer<Throwable> ignore(@Nonnull Consumer<? super Throwable> orElse, @Nonnull Collection<ErrorResponse> set)
{
Checks.notNull(orElse, "Callback");
Checks.notEmpty(set, "Ignored collection");
// Make an enum set copy (for performance, memory efficiency, and thread-safety)
final EnumSet<ErrorResponse> ignored = EnumSet.copyOf(set);
return new ErrorHandler(orElse).ignore(ignored);
}
/**
* An error for a {@link SchemaError}.
* <br>This provides the machine parsable error code name and the human readable message.
*/
public static class ErrorCode
{
private final String code;
private final String message;
ErrorCode(String code, String message)
{
this.code = code;
this.message = message;
}
/**
* The machine parsable error code
*
* @return The error code
*/
@Nonnull
public String getCode()
{
return code;
}
/**
* The human readable explanation message for this error
*
* @return The message
*/
@Nonnull
public String getMessage()
{
return message;
}
@Override
public String toString()
{
return code + ": " + message;
}
}
/**
* Schema error which supplies more context to a ErrorResponse.
* <br>This provides a list of {@link ErrorCode ErrorCodes} and a {@link #getLocation() location} for the errors.
*/
public static class SchemaError
{
private final String location;
private final List<ErrorCode> errors;
private SchemaError(String location, List<ErrorCode> codes)
{
this.location = location;
this.errors = codes;
}
/**
* The JSON-path for the error.
* <br>This path describes the location of the error, within the request json body.
*
* <p><b>Example:</b> {@code embed.fields[3].name}
*
* @return The JSON-path location
*/
@Nonnull
public String getLocation()
{
return location;
}
/**
* The list of {@link ErrorCode ErrorCodes} associated with this schema error.
*
* @return The error codes
*/
@Nonnull
public List<ErrorCode> getErrors()
{
return errors;
}
@Override
public String toString()
{
return (location.isEmpty() ? "" : location+"\n") + "\t- " + errors.stream().map(Object::toString).collect(Collectors.joining("\n\t- "));
}
}
}