24
24
import club .minnced .discord .webhook .send .WebhookMessage ;
25
25
import club .minnced .discord .webhook .send .WebhookMessageBuilder ;
26
26
import club .minnced .discord .webhook .util .ThreadPools ;
27
+ import club .minnced .discord .webhook .util .WebhookErrorHandler ;
27
28
import okhttp3 .OkHttpClient ;
28
29
import okhttp3 .RequestBody ;
29
30
import okhttp3 .Response ;
@@ -55,6 +56,7 @@ public class WebhookClient implements AutoCloseable {
55
56
/** User-Agent used for REST requests */
56
57
public static final String USER_AGENT = "Webhook(https://github.com/MinnDevelopment/discord-webhooks, " + LibraryInfo .VERSION + ")" ;
57
58
private static final Logger LOG = LoggerFactory .getLogger (WebhookClient .class );
59
+ private static WebhookErrorHandler DEFAULT_ERROR_HANDLER = WebhookErrorHandler .DEFAULT ;
58
60
59
61
protected final WebhookClient parent ;
60
62
@@ -70,6 +72,7 @@ public class WebhookClient implements AutoCloseable {
70
72
protected long defaultTimeout ;
71
73
protected volatile boolean isQueued ;
72
74
protected boolean isShutdown ;
75
+ protected WebhookErrorHandler errorHandler = DEFAULT_ERROR_HANDLER ;
73
76
74
77
protected WebhookClient (
75
78
final long id , final String token , final boolean parseMessage ,
@@ -102,6 +105,21 @@ protected WebhookClient(final WebhookClient parent, final long threadId) {
102
105
this .isQueued = false ;
103
106
}
104
107
108
+ /**
109
+ * Configures which default error handler to use instead of {@link WebhookErrorHandler#DEFAULT}.
110
+ *
111
+ * <p>You can use this to avoid having to set the error handler on each new webhook client instance.
112
+ *
113
+ * @param handler
114
+ * The error handler to use
115
+ *
116
+ * @throws NullPointerException
117
+ * If null is povided
118
+ */
119
+ public static void setDefaultErrorHandler (@ NotNull WebhookErrorHandler handler ) {
120
+ DEFAULT_ERROR_HANDLER = Objects .requireNonNull (handler , "Error Handler must not be null!" );
121
+ }
122
+
105
123
/**
106
124
* Factory method to create a basic WebhookClient with the provided id and token.
107
125
*
@@ -238,6 +256,23 @@ public WebhookClient setTimeout(@Nonnegative long millis) {
238
256
return this ;
239
257
}
240
258
259
+ /**
260
+ * Configures the error handling behavior used for all asynchronous send/edit/delete calls.
261
+ *
262
+ * @param handler
263
+ * The error handler
264
+ *
265
+ * @throws java.lang.NullPointerException
266
+ * If provided with null
267
+ *
268
+ * @return The current WebhookClient instance
269
+ */
270
+ @ NotNull
271
+ public WebhookClient setErrorHandler (@ NotNull WebhookErrorHandler handler ) {
272
+ this .errorHandler = Objects .requireNonNull (handler , "Error Handler must not be null!" );
273
+ return this ;
274
+ }
275
+
241
276
/**
242
277
* The current timeout configured by {@link #setTimeout(long)}.
243
278
* <br>If no timeout was configured, this returns 0.
@@ -795,7 +830,7 @@ private boolean executePair(@Async.Execute Request req) {
795
830
}
796
831
else if (!response .isSuccessful ()) {
797
832
final HttpException exception = failure (response );
798
- LOG . error ( "Sending a webhook message failed with non-OK http response" , exception );
833
+ errorHandler . handle ( this , "Sending a webhook message failed with non-OK http response" , exception );
799
834
queue .poll ().future .completeExceptionally (exception );
800
835
return true ;
801
836
}
@@ -812,7 +847,7 @@ else if (!response.isSuccessful()) {
812
847
}
813
848
}
814
849
catch (JSONException | IOException e ) {
815
- LOG . error ( "There was some error while sending a webhook message" , e );
850
+ errorHandler . handle ( this , "There was some error while sending a webhook message" , e );
816
851
queue .poll ().future .completeExceptionally (e );
817
852
}
818
853
return true ;
@@ -832,7 +867,7 @@ public String getMethod() {
832
867
}
833
868
}
834
869
835
- protected static final class Bucket {
870
+ protected final class Bucket {
836
871
public static final int RATE_LIMIT_CODE = 429 ;
837
872
public long resetTime ;
838
873
public int remainingUses ;
@@ -899,7 +934,7 @@ public void update(Response response) {
899
934
update0 (response );
900
935
}
901
936
catch (Exception ex ) {
902
- LOG . error ( "Could not read http response" , ex );
937
+ errorHandler . handle ( WebhookClient . this , "Could not read http response" , ex );
903
938
}
904
939
}
905
940
}
0 commit comments